算法导论学习-插入排序python实现

来源:互联网 发布:好喝的洋酒推荐 知乎 编辑:程序博客网 时间:2024/04/26 02:58
def insertionsort(A):    for j in range(1,len(A)):        key=A[j]        i=j-1        while i>=0 and A[i]>key:            A[i+1]=A[i]            i=i-1#compare with the digital in front of i        A[i+1]=keyA=[15,2,4,6,1,3,12,45]insertionsort(A)

print(A)

输出结果:

[1, 2, 3, 4, 6, 12, 15, 45]