算法导论:插入排序算法之python实现

A = [99, 38, 65, 97, 76, 13, 27, 49]
# 排序过程
# A = [38, 99, 65, 97, 76, 13, 27, 49]
# A = [38, 65, 99, 97, 76, 13, 27, 49]
# A = [38, 65, 76, 97, 99, 13, 27, 49]
# A = [13, 38, 65, 76, 97, 99, 27, 49]
# A = [13, 27, 38, 65, 76, 97, 99, 49]
# A = [13, 27, 38, 65, 76, 97, 49, 99]


def insertsort(A):
    length = len(A)
    for j in range(1, length):
        key = A[j]
        i = j - 1
        while i >= 0 and A[i] > key:
            A[i + 1] = A[i]
            i = i - 1
        A[i + 1] = key


insertsort(A)

print(A)

你可能感兴趣的:(算法导论)