美文网首页
直接插入排序

直接插入排序

作者: qianyewhy | 来源:发表于2017-09-11 06:14 被阅读10次
    
    def straightInsert(a):
        # 直接插入排序: 小->大
        for i in range(1, len(a)):    # 待排序
            index = a[i]
            j = i - 1        # 表示已经排好序
            while j >= 0 and a[j] > index:
                a[j + 1] = a[j]
                j -= 1
            a[j + 1] = index
            print(a)
    a = [5, 4, 3, 2, 1]
    straightInsert(a)
    

    相关文章

      网友评论

          本文标题:直接插入排序

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