选择排序
- 选择排序对冒泡排序进行了改进,每趟都使当前最大项就位
- 选择排序的时间复杂度比冒泡排序稍优:
对比次数是O( ),交换次数减少为O(n)
def selectionSort(alist): for fillslot in range(len(alist)-1,0,-1): positionOfMax = 0 for location in range(1, fillslot + 1): if alist[location] > alist[positionOfMax]: positionOfMax = location alist[location],alist[positionOfMax] = alist[positionOfMax], alist[location]