from random import randint
from timeit import repeat
# 运行排序算法并计算耗时
def run_sorting_algorithm(algorithm, array):
setup_code = f"from __main__ import {algorithm}" \
if algorithm != "sorted" else ""
stmt = f"{algorithm}({array})"
times = repeat(setup=setup_code, stmt=stmt, repeat=3, number=1)
print(f"Algorithm: {algorithm}. Minimum execution time: {min(times)}")
# bubble_sort 冒泡
def bubble_sort(array):
n = len(array)
for i in range(n):
already_sorted = True
for j in range(n - i - 1):
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
already_sorted = False
if already_sorted:
break
return array
# insertion_sort 插入
def insertion_sort(array):
for i in range(1, len(array)):
key_item = array[i]
j = i - 1
while j >= 0 and array[j] > key_item:
array[j + 1] = array[j]
j -= 1
array[j + 1] = key_item
return array
# merge_sort 归并
def merge(left, right):
if len(left) == 0:
return right
if len(right) == 0:
return left
result = []
index_left = index_right = 0
while len(result) < len(left) + len(right):
if left[index_left] <= right[index_right]:
result.append(left[index_left])
index_left += 1
else:
result.append(right[index_right])
index_right += 1
if index_right == len(right):
result += left[index_left:]
break
if index_left == len(left):
result += right[index_right:]
break
return result
def merge_sort(array):
if len(array) < 2:
return array
midpoint = len(array) // 2
return merge(
left=merge_sort(array[:midpoint]),
right=merge_sort(array[midpoint:]))
# quicksort 快速
def quicksort(array):
if len(array) < 2:
return array
low, same, high = [], [], []
pivot = array[randint(0, len(array) - 1)]
for item in array:
if item < pivot:
low.append(item)
elif item == pivot:
same.append(item)
elif item > pivot:
high.append(item)
return quicksort(low) + same + quicksort(high)
# timsort
def timsort_insertion_sort(array, left=0, right=None):
if right is None:
right = len(array) - 1
for i in range(left + 1, right + 1):
key_item = array[i]
j = i - 1
while j >= left and array[j] > key_item:
array[j + 1] = array[j]
j -= 1
array[j + 1] = key_item
return array
def timsort(array):
min_run = 32
n = len(array)
for i in range(0, n, min_run):
timsort_insertion_sort(array, i, min((i + min_run - 1), n - 1))
size = min_run
while size < n:
for start in range(0, n, size * 2):
midpoint = start + size - 1
end = min((start + size * 2 - 1), (n-1))
merged_array = merge(
left=array[start:midpoint + 1],
right=array[midpoint + 1:end + 1])
array[start:start + len(merged_array)] = merged_array
size *= 2
return array
ARRAY_LENGTH = 10000
if __name__ == "__main__":
array = [randint(0, 1000) for i in range(ARRAY_LENGTH)]
run_sorting_algorithm(algorithm="sorted", array=array)
run_sorting_algorithm(algorithm="bubble_sort", array=array)
run_sorting_algorithm(algorithm="insertion_sort", array=array)
run_sorting_algorithm(algorithm="merge_sort", array=array)
run_sorting_algorithm(algorithm="quicksort", array=array)
run_sorting_algorithm(algorithm="timsort", array=array)
输出
Algorithm: sorted. Minimum execution time: 0.0013730359999999942
Algorithm: bubble_sort. Minimum execution time: 8.838670004999997
Algorithm: insertion_sort. Minimum execution time: 4.349029395999999
Algorithm: merge_sort. Minimum execution time: 0.06647415900000198
Algorithm: quicksort. Minimum execution time: 0.01507530800000012
Algorithm: timsort. Minimum execution time: 0.05889881199999536
代码参考:
https://realpython.com/sorting-algorithms-python/
timeit包用法参考:
https://www.cnblogs.com/cicaday/p/12431380.html
算法介绍:
https://www.cnblogs.com/onepixel/articles/7674659.html