Python代码:
def insertion_sort(list):
for index in range(1,len(list)):
value = list[index]
i = index -1
while i>=0 and value < list[i]:
if value <list[i]:
list[i+1] =list[i]
list[i] = value
i = i -1
a = [2,5,3,4,1,7]
insertion_sort(a)
print(a)
结果:
[1, 2, 3, 4, 5, 7]