常见排序算法
Posted 专治spark
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常见排序算法相关的知识,希望对你有一定的参考价值。
常见排序算法有很多种,这里仅列举冒泡排序、插入排序、选择排序、归并排序。
冒泡排序:每一轮排序得出一个最大值
1 # for实现 2 lst = [7,9,3,11,45,23,12,87,234,12,5,93] 3 n = len(lst) 4 5 for i in range(n-1): 6 flag = True 7 for j in range(n-1-i): 8 if lst[j] > lst[j+1]: 9 lst[j],lst[j+1] = lst[j+1],lst[j] 10 flag = False 11 if flag : 12 break 13 14 print(‘lst: ‘,lst) 15 16 # while实现 17 lst1 = [7,9,3,11,45,23,12,87,234,12,5,93] 18 i = 1 19 n = len(lst1) 20 21 while i < n: 22 j = 0 23 flag = True 24 while j < n-i: 25 if lst1[j] > lst1[j+1]: 26 lst1[j],lst1[j+1] = lst1[j+1],lst1[j] 27 flag = False 28 j += 1 29 if flag: 30 break 31 i += 1 32 33 print(‘lst1:‘,lst1)
运行结果:
lst: [3, 5, 7, 9, 11, 12, 12, 23, 45, 87, 93, 234]
lst1: [3, 5, 7, 9, 11, 12, 12, 23, 45, 87, 93, 234]
插入排序:每次取一个新数字与已取过数字比较,每轮后取过的数字都排好顺序
1 lst = [3,88,76,23,999,22,888,56,39,72] 2 3 for i in range(len(lst)): 4 key = lst[i] 5 j = i-1 6 while j >= 0 and lst[j]> key: 7 lst[j],lst[j+1] = lst[j+1],lst[j] 8 j -= 1 9 10 print(‘lst: ‘,lst)
运行结果:
lst: [3, 22, 23, 39, 56, 72, 76, 88, 888, 999]
选择排序:每一轮排出一个最小值
1 lst = [7,9,3,11,45,23,12,87,234,11,5,93] 2 n = len(lst) 3 4 for i in range(n-1): 5 small = i 6 for j in range(i+1,n): 7 if lst[j] < lst[small]: 8 small = j 9 if small != i: 10 lst[i],lst[small] = lst[small],lst[i] 11 12 print(‘lst: ‘,lst)
运行结果:
lst: [3, 5, 7, 9, 11, 11, 12, 23, 45, 87, 93, 234]
归并排序:原则是用递归先将序列分成最小两单元,然后排序再合并。。。最后将排好序的两个子序列再排序合并成一个有序序列
1 def merge(a, b): 2 c = [] 3 h = j = 0 4 while j < len(a) and h < len(b): 5 if a[j] < b[h]: 6 c.append(a[j]) 7 j += 1 8 else: 9 c.append(b[h]) 10 h += 1 11 12 if j == len(a): 13 for i in b[h:]: 14 c.append(i) 15 else: 16 for i in a[j:]: 17 c.append(i) 18 19 return c 20 21 def merge_sort(lists): 22 if len(lists) <= 1: 23 return lists 24 mid = len(lists)//2 25 left = merge_sort(lists[:mid]) 26 right = merge_sort(lists[mid:]) 27 return merge(left, right) 28 29 if __name__ == ‘__main__‘: 30 lst = [20,5,12,9,18,25,20,3] 31 print(merge_sort(lst))
运行结果:
[3, 5, 9, 12, 18, 20, 20, 25]
以上是关于常见排序算法的主要内容,如果未能解决你的问题,请参考以下文章