python学习之实现5种排序算法
Posted 今夜月色很美
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习之实现5种排序算法相关的知识,希望对你有一定的参考价值。
冒泡排序
from timeit import Timer
# 冒泡排序
# 每轮循环比较交换相邻两个数的值,把最大值放在末尾,就像冒泡
def bubble_sort(arr):
for i in range(len(arr)):
for j in range(len(arr) - i - 1):
if arr[j] > arr[j + 1]:
# print("i=%s,j=%s,%s" % (i, j, arr))
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
print(bubble_sort([986, 980, 125, 605, 787, 211, 849, 21, 643, 97, 88, 433, 705, 392, 954, 264, 499, 0, 838, 745]))
t1 = Timer("bubble_sort([388, 794, 673, 566, 219, 428, 64, 716, 39, 432, 249, 877, 222, 339, 943, 76, 25, 822, 13, "
"446])", "from __main__ import bubble_sort")
# 20个随机数排列整齐,10000次排序完成需要时间约0.2666784
print(t1.timeit(10000))
选择排序
from timeit import Timer
# 选择排序
# 每轮循环选出最大值放在末尾
def select_sort(arr):
for i in range(len(arr)):
max_index = 0
for j in range(0, len(arr) - i):
# print("i=%s,j=%s,max_index=%s,%s" % (i, j, max_index, arr))
if arr[j] > arr[max_index]:
max_index = j
arr[len(arr) - i - 1], arr[max_index] = arr[max_index], arr[len(arr) - i - 1]
return arr
print(select_sort([986, 980, 125, 605, 787, 211, 849, 21, 643, 97, 88, 433, 705, 392, 954, 264, 499, 0, 838, 745]))
time = Timer("select_sort([388, 794, 673, 566, 219, 428, 64, 716, 39, 432, 249, 877, 222, 339, 943, 76, 25, 822, 13, "
"446])", "from __main__ import select_sort")
# 20个随机数排列整齐,10000次排序完成需要时间约0.193074
print(time.timeit(10000))
插入排序
from timeit import Timer
# 插入排序
# 先使前n个数有序,然后逐个加入元素
def insert_sort(arr):
# order_last有序列表最后一个数的下标
order_last = 0
for i in range(1, len(arr)):
value = arr[i]
# 从后往前遍历有序列表的每个元素,如果比新加入元素大,则向后移动一位
for j in range(order_last, -1, -1):
# print("i=%s,j=%s,%s" % (i, j, arr))
if arr[j] > value:
arr[j + 1] = arr[j]
arr[j] = value
# 内层循环结束,有序列表多了一个元素,最后一个数的下标往后移
order_last = order_last + 1
return arr
print(insert_sort([986, 980, 125, 605, 787, 211, 849, 21, 643, 97, 88, 433, 705, 392, 954, 264, 499, 0, 838, 745]))
time = Timer("insert_sort([388, 794, 673, 566, 219, 428, 64, 716, 39, 432, 249, 877, 222, 339, 943, 76, 25, 822, 13, "
"446])", "from __main__ import insert_sort")
# 20个随机数排列整齐,10000次排序完成需要时间约0.204921
print(time.timeit(10000))
归并排序
from timeit import Timer
# 归并排序
# 递归分成左右两个子集,直至每个子集元素个数为1,然后将左右子集有序merge
def merge_sort(arr):
if len(arr) > 1:
merged = []
# 第一步,分裂
mid = len(arr) // 2
# print("原数组:%s, mid=%s,左子集:%s,右子集:%s" % (arr, mid, arr[:mid], arr[mid:]))
left_arr = merge_sort(arr[:mid])
right_arr = merge_sort(arr[mid:])
# print("排序后:%s, mid=%s,左子集:%s,右子集:%s" % (arr, mid, arr[:mid], arr[mid:]))
# 第二步,合并
while left_arr and right_arr:
if left_arr[0] < right_arr[0]:
merged.append(left_arr.pop(0))
else:
merged.append(right_arr.pop(0))
merged.extend(left_arr if left_arr else right_arr)
# print("合并后:%s" % merged)
return merged
else:
return arr
print(merge_sort([986, 980, 125, 605, 787, 211, 849, 21, 643, 97, 88, 433, 705, 392, 954, 264, 499, 0, 838, 745]))
time = Timer("merge_sort([388, 794, 673, 566, 219, 428, 64, 716, 39, 432, 249, 877, 222, 339, 943, 76, 25, 822, 13, "
"446])", "from __main__ import merge_sort")
# 20个随机数排列整齐,10000次排序完成需要时间0.2543816
print(time.timeit(10000))
快速排序
from timeit import Timer
# 快速排序
# 每轮循环选出最大值放在末尾
def quick_sort(arr, left, right):
if left >= right:
return arr
# 取列表中的第一个数做中值
mid = arr[left]
start = left + 1
end = right
while start <= end:
while start <= end and arr[start] <= mid:
start = start + 1
while start <= end and arr[end] >= mid:
end = end - 1
if start > end:
break
else:
arr[start],arr[end] = arr[end],arr[start]
# print("before====mid=%s,left=%s,right=%s,%s" % (left, left, right, arr))
arr[left],arr[end] = arr[end],arr[left]
# print("after====mid=%s,left=%s,right=%s,%s" % (end, left, right, arr))
quick_sort(arr, left, end - 1)
quick_sort(arr, end + 1, right)
return arr
print(quick_sort([986, 980, 125, 605, 787, 211, 849, 21, 643, 97, 88, 433, 705, 392, 954, 264, 499, 0, 838, 745], 0, 19))
time = Timer("quick_sort([388, 794, 673, 566, 219, 428, 64, 716, 39, 432, 249, 877, 222, 339, 943, 76, 25, 822, 13, "
"446], 0, 19)", "from __main__ import quick_sort")
# 20个随机数排列整齐,10000次排序完成需要时间0.1195915
print(time.timeit(10000))
以上是关于python学习之实现5种排序算法的主要内容,如果未能解决你的问题,请参考以下文章