Internal sorting algorithm Summary

Direct insertion sort

Time complexity: O (n ^ 2)
first as a first sequence is an ordered sequence, and then inserted from the second recording start one by one, to find the insertion position of the note from the rear forward.

Sample code:

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

Guess you like

Origin www.cnblogs.com/ldy-miss/p/12026376.html