python 选择排序

2020-07-22  本文已影响0人  王宣成
#!/usr/bin/python3
import random

# 创建无序的列表
def randomList(n):
    iList = []
    for i in range(n):
        iList.append(random.randrange(1000))
    return  iList

iList = randomList(20)

def selectionSort(iList):
    if len(iList) <= 1:
        return  iList

    print(iList)

    for i in range(len(iList)):
        if iList[i] != min(iList[i:]):
            minIndex = iList.index(min(iList[i:]))
            iList[i],iList[minIndex] = iList[minIndex],iList[i]
    print(iList)

    return  iList

if __name__ == "__main__":
    selectionSort(iList)

上一篇下一篇

猜你喜欢

热点阅读