标签:函数 logs and pre 排序 int 迭代 print log
插入排序:
def insertion(lst):
for i in range(1,len(lst)):
save = lst[i]
j = i
while j>0 and lst[j-1]>save:
lst[j] = lst[j-1]
j-=1
lst[j] = save
print(lst)
insertion([9,5,2,3,4,1,6,8,7,10])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1.定义insertion函数
2.迭代i的长度
? 将每一次迭代的列表值赋给变量save
??将i赋给j
3.如果j>0 且 lst[j-1]>save(lst[j]):后面一位大于前面一位,然后大的值后移赋值
??lst[j] = lst[j-1]
j -= 1#每次减1 直到前一位小于后一位 save赋值给lst[j]
lst[j] = save
标签:函数 logs and pre 排序 int 迭代 print log
原文地址:http://www.cnblogs.com/hkcs/p/7646692.html