def straightInsert(a):
# 直接插入排序: 小->大
for i in range(1, len(a)): # 待排序
index = a[i]
j = i - 1 # 表示已经排好序
while j >= 0 and a[j] > index:
a[j + 1] = a[j]
j -= 1
a[j + 1] = index
print(a)
a = [5, 4, 3, 2, 1]
straightInsert(a)
网友评论