美文网首页
插入排序

插入排序

作者: 谁更勇猛 | 来源:发表于2017-02-06 08:25 被阅读11次

    Python代码

    def insertionSort(numbers) : 
        length = len(numbers)
        if length > 1:
            i = 1
            for i in range(length):
                key = numbers[i]
                j = i - 1
                while j >= 0 and numbers[j] > key :
                    numbers[j + 1] = numbers[j]
                    j = j - 1
                numbers[j + 1] = key
    

    输入:[6, 1, 3, 4, 2, 5]
    输出:[1, 2, 3, 4, 5, 6]

    最好情况:an+b
    最坏情况:an^2+bn+c
    (a、b、c都是依赖于代价c的常量)

    相关文章

      网友评论

          本文标题:插入排序

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