内部排序算法汇总

直接插入排序

时间复杂度:O(n^2)
先将序列中第1个看成是一个有序的子序列,然后从第2个记录开始起逐个进行插入,注意从后向前查找插入位置。

示例代码:

def insert_sort(l):
    for i in range(1, len(l)):    # 从第2个记录开始逐个进行插入
        j = i - 1
        temp = l[i]
        while j >= 0 and l[j] > temp:
            l[j + 1] = l[j]
            j -= 1
        l[j + 1] = temp
    return l

猜你喜欢

转载自www.cnblogs.com/ldy-miss/p/12026376.html