Skip to content

Commit e4cc486

Browse files
committed
'sort'
0 parents  commit e4cc486

25 files changed

+1078
-0
lines changed

.idea/dictionaries/snows.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sort.iml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 773 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sort/BubbleSort.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'''冒泡排序 '''
5+
6+
7+
def bubble_sort(list):
8+
length = len(list)
9+
for index in range(length):
10+
for j in range(1, length - index):
11+
if list[j - 1] > list[j]:
12+
# 交换两者数据,这里没用temp是因为python 特性元组。
13+
list[j - 1], list[j] = list[j], list[j - 1]
14+
return list
15+
16+
17+
def bubble_sort_flag(list):
18+
length = len(list)
19+
for index in range(length):
20+
# 标志位
21+
flag = True
22+
for j in range(1, length - index):
23+
if list[j - 1] > list[j]:
24+
list[j - 1], list[j] = list[j], list[j - 1]
25+
flag = False
26+
if flag:
27+
# 没有发生交换,直接返回list
28+
return list
29+
return list

Sort/CountSort.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
def count_sort(list):
5+
min = 2147483647
6+
max = 0
7+
# 第一步 取得最大值和最小值
8+
for x in list:
9+
if x < min:
10+
min = x
11+
if x > max:
12+
max = x
13+
# 创建数组C
14+
count = [0] * (max - min +1)
15+
for index in list:
16+
count[index - min] += 1
17+
index = 0
18+
for a in range(max - min+1):
19+
for c in range(count[a]):
20+
list[index] = a + min
21+
index += 1
22+
return list

Sort/HeapSort.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
5+
def heap_sort(list):
6+
# 创建最大堆
7+
for start in range((len(list) - 2) // 2, -1, -1):
8+
sift_down(list, start, len(list) - 1)
9+
10+
# 堆排序
11+
for end in range(len(list) - 1, 0, -1):
12+
list[0], list[end] = list[end], list[0]
13+
sift_down(list, 0, end - 1)
14+
return list
15+
16+
17+
# 最大堆调整
18+
def sift_down(lst, start, end):
19+
root = start
20+
while True:
21+
child = 2 * root + 1
22+
if child > end:
23+
break
24+
if child + 1 <= end and lst[child] < lst[child + 1]:
25+
child += 1
26+
if lst[root] < lst[child]:
27+
lst[root], lst[child] = lst[child], lst[root]
28+
root = child
29+
else:
30+
break

Sort/InsertSort.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
5+
6+
def insert_sort(list):
7+
n = len(list)
8+
for i in range(1, n):
9+
# 后一个元素和前一个元素比较
10+
# 如果比前一个小
11+
if list[i] < list[i - 1]:
12+
# 将这个数取出
13+
temp = list[i]
14+
# 保存下标
15+
index = i
16+
# 从后往前依次比较每个元素
17+
for j in range(i - 1, -1, -1):
18+
# 和比取出元素大的元素交换
19+
if list[j] > temp:
20+
list[j + 1] = list[j]
21+
index = j
22+
else:
23+
break
24+
# 插入元素
25+
list[index] = temp
26+
return list

Sort/MargeSort.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
def merge_sort(list):
5+
# 认为长度不大于1的数列是有序的
6+
if len(list) <= 1:
7+
return list
8+
# 二分列表
9+
middle = len(list) // 2
10+
left = merge_sort(list[:middle])
11+
right = merge_sort(list[middle:])
12+
# 最后一次合并
13+
return merge(left, right)
14+
15+
16+
# 合并
17+
def merge(left, right):
18+
l, r = 0, 0
19+
result = []
20+
# 两个子数列比大小
21+
while l < len(left) and r < len(right):
22+
if left[l] < right[r]:
23+
result.append(left[l])
24+
l += 1
25+
else:
26+
result.append(right[r])
27+
r += 1
28+
# 填充结果
29+
result += left[l:]
30+
result += right[r:]
31+
return result

0 commit comments

Comments
 (0)