Skip to content

Commit e4cc486

Browse files
committed
'sort'
0 parents  commit e4cc486

25 files changed

+1078
-0
lines changed

Diff for: .idea/dictionaries/snows.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/misc.xml

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/sort.iml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/workspace.xml

+773
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Sort/BubbleSort.py

+29
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

Diff for: Sort/CountSort.py

+22
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

Diff for: Sort/HeapSort.py

+30
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

Diff for: Sort/InsertSort.py

+26
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

Diff for: Sort/MargeSort.py

+31
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

Diff for: Sort/QuickSort.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
5+
6+
def quick_sort(list):
7+
less = []
8+
pivotList = []
9+
more = []
10+
# 递归出口
11+
if len(list) <= 1:
12+
return list
13+
else:
14+
# 将第一个值做为基准
15+
pivot = list[0]
16+
for i in list:
17+
# 将比急转小的值放到less数列
18+
if i < pivot:
19+
less.append(i)
20+
# 将比基准打的值放到more数列
21+
elif i > pivot:
22+
more.append(i)
23+
# 将和基准相同的值保存在基准数列
24+
else:
25+
pivotList.append(i)
26+
# 对less数列和more数列继续进行排序
27+
less = quick_sort(less)
28+
more = quick_sort(more)
29+
return less + pivotList + more
30+
31+
32+
def qsort(arr):
33+
if len(arr) <= 1:
34+
return arr
35+
else:
36+
pivot = arr[0]
37+
return qsort([x for x in arr[1:] if x < pivot]) + \
38+
[pivot] + \
39+
qsort([x for x in arr[1:] if x >= pivot])

Diff for: Sort/SelectionSort.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
5+
def selection_sort(list):
6+
n = len(list)
7+
# 遍历所有元素
8+
for i in range(0, n):
9+
# 获取最小元素的
10+
min = i
11+
# 遍历未排序元素
12+
for j in range(i + 1, n):
13+
# 找到一个比最小元素小的元素
14+
if list[j] < list[min]:
15+
min = j
16+
# 交换数据
17+
list[min], list[i] = list[i], list[min]
18+
return list

Diff for: Sort/ShellSort.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
5+
def shell_sort(list):
6+
n = len(list)
7+
# 初始步长
8+
gap = round(n / 2)
9+
while gap > 0:
10+
for i in range(gap, n):
11+
# 每个步长进行插入排序
12+
temp = list[i]
13+
j = i
14+
# 插入排序
15+
while j >= gap and list[j - gap] > temp:
16+
list[j] = list[j - gap]
17+
j -= gap
18+
list[j] = temp
19+
# 得到新的步长
20+
gap = round(gap / 2)
21+
return list

Diff for: Sort/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'''a test module '''
5+
6+
__author__ = 'EINDEX'

Diff for: Sort/__pycache__/BubbleSort.cpython-35.pyc

758 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/CountSort.cpython-35.pyc

530 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/HeapSort.cpython-35.pyc

768 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/InsertSort.cpython-35.pyc

474 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/MargeSort.cpython-35.pyc

734 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/QuickSort.cpython-35.pyc

1004 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/SelectionSort.cpython-35.pyc

423 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/ShellSort.cpython-35.pyc

489 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/__init__.cpython-35.pyc

191 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/main.cpython-35.pyc

1.49 KB
Binary file not shown.

Diff for: Sort/main.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'''测试函数 '''
5+
import random
6+
import time
7+
8+
from Sort.BubbleSort import bubble_sort, bubble_sort_flag
9+
from Sort.CountSort import count_sort
10+
from Sort.HeapSort import heap_sort
11+
from Sort.InsertSort import insert_sort
12+
from Sort.MargeSort import merge_sort
13+
from Sort.QuickSort import quick_sort, qsort
14+
from Sort.SelectionSort import selection_sort
15+
from Sort.ShellSort import shell_sort
16+
import sys
17+
18+
# 开挂 防止栈溢出
19+
sys.setrecursionlimit(99999)
20+
21+
#整数数列
22+
# L = [int(random.uniform(0, 5000)) for x in range(1000)]
23+
# 浮点数列
24+
L = [random.uniform(0, 5000) for x in range(1000)]
25+
26+
current = list(L)
27+
current.sort()
28+
29+
def compile(fun):
30+
l = list(L)
31+
start=time.time()
32+
res = fun(l)
33+
over = time.time()
34+
print('time:'+str(over-start),current == res, fun.__name__)
35+
36+
37+
compile(selection_sort)
38+
compile(bubble_sort)
39+
compile(bubble_sort_flag)
40+
compile(insert_sort)
41+
compile(merge_sort)
42+
compile(shell_sort)
43+
compile(quick_sort)
44+
compile(qsort)
45+
compile(heap_sort)
46+
if isinstance(L[0],int):
47+
compile(count_sort)

0 commit comments

Comments
 (0)