zoukankan      html  css  js  c++  java
  • 10-选择排序

    # 选择排序对冒泡排序进行了改进,保留了其基本的多趟比对思路,每趟都使当前最大项就位
    # 但选择排序对交换进行了削减,相比起冒泡排序进行多次交换,每趟仅进行1次交换,记录最大项的所在位置,最后再跟本趟最后一项交换
    # 选择排序的时间复杂度比冒泡排序稍优
    # 比对次数不变,还是O(n2)
    # 交换次数则减少为O(n)
    
    
    def selectionSort(alist):
        for fillslot in range(len(alist)-1, 0, -1):
            positionOfMax = 0
            for location in range(1, fillslot+1):  # 从1开始是因为0已经赋值给positionOfMax
                if alist[location] > alist[positionOfMax]:
                    positionOfMax = location
                    alist[fillslot], alist[positionOfMax] = alist[positionOfMax], alist[fillslot]
    
    
    testlist = [2, 3, 3, 243, 24, 24455, 23]
    selectionSort(testlist)
    print(testlist)
    
  • 相关阅读:
    UVa 106
    UVa 111
    UVa 105
    UVa 104
    UVa 103
    UVa 102
    UVa 101
    UVa 100
    就决定是你了!
    阿姆斯特朗回旋加速喷气式阿姆斯特朗炮
  • 原文地址:https://www.cnblogs.com/lotuslaw/p/13968810.html
Copyright © 2011-2022 走看看