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 insertionSort(iList):
    if len(iList) <= 1:
        return  iList

    print(iList)

    for right in range(1,len(iList)):
        target = iList[right]
        for left in range(0,right):
            if target <= iList[left]:
                iList[left+1:right+1] = iList[left:right]
                iList[left] = target
                break;

    print(iList)
    return  iList

if __name__ == "__main__":
    insertionSort(iList)
上一篇下一篇

猜你喜欢

热点阅读