刷题总结
ThinkTimes
这个作者很懒,什么都没留下…
展开
-
快速排序
# 递归,终止条件?一步的操作?返回什么?def quickSort(aList): quickSortHelper(aList, 0, len(aList)-1)def quickSortHelper(aList, first, last): if first < last: splitPosition = partition(aList, first, last) quickSortHelper(aList, first,splitPosit.原创 2020-12-01 21:16:39 · 115 阅读 · 0 评论 -
插入排序
# 希尔排序可以理解为不同间隔步的插排def shellSort(aList): subListCount = len(aList) // 2 while subListCount > 0: for startPosition in range(subListCount): gapInsertSort(aList, startPosition, subListCount) print("After increments of .原创 2020-12-01 21:14:35 · 97 阅读 · 0 评论 -
归并排序
# 递归,先分,后合,合的时候就控制下顺序# 递归: 终止条件? 一步的操作?返回什么?def mergeSort(aList): print("Now list is ",aList) if len(aList) > 1: middle = len(aList) //2 lefthalf = aList[ : middle] rightHalf = aList[middle : ] mergeSort(leftha.原创 2020-12-01 21:13:23 · 96 阅读 · 0 评论 -
插入排序
# 与选择排序相比,交换操作变成了移位操作(交换比移位耗时)所以时间较短def insertMinSort(aList): for i in range(1,len(aList)): currentValue = aList[i] position = i while position > 0 and currentValue < aList[position - 1]: aList[position] = a.原创 2020-11-30 13:47:14 · 96 阅读 · 0 评论 -
选择排序
# 选择最大的往后排,最后i个是有序列表def selectMaxSort(aList): for i in range(len(aList)): maxNum = aList[0] maxLoc = 0 for j in range(len(aList)-i): if aList[j] > maxNum: maxNum = aList[j] maxLoc.原创 2020-11-30 10:03:53 · 114 阅读 · 0 评论 -
冒泡排序
def bubbleSort(aList): for i in range(len(aList)): for j in range(len(aList) - i - 1): if aList[j] > aList[j + 1]: aList[j], aList[j + 1] = aList[j + 1], aList[j]alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]b.原创 2020-11-30 09:36:51 · 125 阅读 · 0 评论