美文网首页
python 插入排序

python 插入排序

作者: 王宣成 | 来源:发表于2020-07-22 22:30 被阅读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)
    

    相关文章

      网友评论

          本文标题:python 插入排序

          本文链接:https://www.haomeiwen.com/subject/fhjxlktx.html