插入排序
- 初始时手里(有序区)只有一张牌
- 每次从无序区摸一张牌,插入到手里已有牌的正确位置
- 时间复杂度O(n^2)
def Insert_sort(li):
for i in range(1,len(li)):
tem = li[i] # 摸的牌
for j in range(i-1,-1,-1):
if li[j] > tem:
li[j+1] = li[j] # 大的牌往右挪一步
li[j] = tem # 摸的牌插入到当前位置
return li
li = [7,3,5,6,1,8,4,9,2,10,15,13,11,12]
Insert_sort(li)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15]