‘’
以下是我对网上说的插入排序的理解,并做了优化的代码。
‘’
如下:
lis = [19,43,86,5,96,25,75,84,12,45,9,43,83,53]
print("%s\n=================" % lis)
def pop_sort(lis):
count = len(lis) # 计算列表个数
for i in range(1,count):
k = lis[i]
j = i - 1
print("比较第%s个数与之前的数" % i)
print(lis)
while j >= 0 :
if lis[j] > k:
lis[j+1] = lis[j]
lis[j] = k
else:
print(" %s 不小于 %s , 停止对比 , 跳出此次循环" % (k, lis[j]))
break
j -= 1
print(lis)
return lis
pop_sort(lis)
print(lis)
输出结果, list 第一位下标为0,即下方第一行代码的19。
[19, 43, 86, 5, 96, 25, 75, 84, 12, 45, 9, 43, 83, 53]
=================
比较第1个数与之前的数
[19, 43, 86, 5, 96, 25, 75, 84, 12, 45, 9, 43, 83, 53]
43 不小于 19 , 停止对比 , 跳出此次循环
比较第2个数与之前的数
[19, 43, 86, 5, 96, 25, 75, 84, 12, 45, 9, 43, 83, 53]
86 不小于 43 , 停止对比 , 跳出此次循环
比较第3个数与之前的数
[19, 43, 86, 5, 96, 25, 75, 84, 12, 45, 9, 43, 83, 53]
[19, 43, 5, 86, 96, 25, 75, 84, 12, 45, 9, 43, 83, 53]
[19, 5, 43, 86, 96, 25, 75, 84, 12, 45, 9, 43, 83, 53]
[5, 19, 43, 86, 96, 25, 75, 84, 12, 45, 9, 43, 83, 53]
比较第4个数与之前的数
[5, 19, 43, 86, 96, 25, 75, 84, 12, 45, 9, 43, 83, 53]
96 不小于 86 , 停止对比 , 跳出此次循环
比较第5个数与之前的数
[5, 19, 43, 86, 96, 25, 75, 84, 12, 45, 9, 43, 83, 53]
[5, 19, 43, 86, 25, 96, 75, 84, 12, 45, 9, 43, 83, 53]
[5, 19, 43, 25, 86, 96, 75, 84, 12, 45, 9, 43, 83, 53]
[5, 19, 25, 43, 86, 96, 75, 84, 12, 45, 9, 43, 83, 53]
25 不小于 19 , 停止对比 , 跳出此次循环
比较第6个数与之前的数
[5, 19, 25, 43, 86, 96, 75, 84, 12, 45, 9, 43, 83, 53]
[5, 19, 25, 43, 86, 75, 96, 84, 12, 45, 9, 43, 83, 53]
[5, 19, 25, 43, 75, 86, 96, 84, 12, 45, 9, 43, 83, 53]
75 不小于 43 , 停止对比 , 跳出此次循环
比较第7个数与之前的数
[5, 19, 25, 43, 75, 86, 96, 84, 12, 45, 9, 43, 83, 53]
[5, 19, 25, 43, 75, 86, 84, 96, 12, 45, 9, 43, 83, 53]
[5, 19, 25, 43, 75, 84, 86, 96, 12, 45, 9, 43, 83, 53]
84 不小于 75 , 停止对比 , 跳出此次循环
比较第8个数与之前的数
[5, 19, 25, 43, 75, 84, 86, 96, 12, 45, 9, 43, 83, 53]
[5, 19, 25, 43, 75, 84, 86, 12, 96, 45, 9, 43, 83, 53]
[5, 19, 25, 43, 75, 84, 12, 86, 96, 45, 9, 43, 83, 53]
[5, 19, 25, 43, 75, 12, 84, 86, 96, 45, 9, 43, 83, 53]
[5, 19, 25, 43, 12, 75, 84, 86, 96, 45, 9, 43, 83, 53]
[5, 19, 25, 12, 43, 75, 84, 86, 96, 45, 9, 43, 83, 53]
[5, 19, 12, 25, 43, 75, 84, 86, 96, 45, 9, 43, 83, 53]
[5, 12, 19, 25, 43, 75, 84, 86, 96, 45, 9, 43, 83, 53]
12 不小于 5 , 停止对比 , 跳出此次循环
比较第9个数与之前的数
[5, 12, 19, 25, 43, 75, 84, 86, 96, 45, 9, 43, 83, 53]
[5, 12, 19, 25, 43, 75, 84, 86, 45, 96, 9, 43, 83, 53]
[5, 12, 19, 25, 43, 75, 84, 45, 86, 96, 9, 43, 83, 53]
[5, 12, 19, 25, 43, 75, 45, 84, 86, 96, 9, 43, 83, 53]
[5, 12, 19, 25, 43, 45, 75, 84, 86, 96, 9, 43, 83, 53]
45 不小于 43 , 停止对比 , 跳出此次循环
比较第10个数与之前的数
[5, 12, 19, 25, 43, 45, 75, 84, 86, 96, 9, 43, 83, 53]
[5, 12, 19, 25, 43, 45, 75, 84, 86, 9, 96, 43, 83, 53]
[5, 12, 19, 25, 43, 45, 75, 84, 9, 86, 96, 43, 83, 53]
[5, 12, 19, 25, 43, 45, 75, 9, 84, 86, 96, 43, 83, 53]
[5, 12, 19, 25, 43, 45, 9, 75, 84, 86, 96, 43, 83, 53]
[5, 12, 19, 25, 43, 9, 45, 75, 84, 86, 96, 43, 83, 53]
[5, 12, 19, 25, 9, 43, 45, 75, 84, 86, 96, 43, 83, 53]
[5, 12, 19, 9, 25, 43, 45, 75, 84, 86, 96, 43, 83, 53]
[5, 12, 9, 19, 25, 43, 45, 75, 84, 86, 96, 43, 83, 53]
[5, 9, 12, 19, 25, 43, 45, 75, 84, 86, 96, 43, 83, 53]
9 不小于 5 , 停止对比 , 跳出此次循环
比较第11个数与之前的数
[5, 9, 12, 19, 25, 43, 45, 75, 84, 86, 96, 43, 83, 53]
[5, 9, 12, 19, 25, 43, 45, 75, 84, 86, 43, 96, 83, 53]
[5, 9, 12, 19, 25, 43, 45, 75, 84, 43, 86, 96, 83, 53]
[5, 9, 12, 19, 25, 43, 45, 75, 43, 84, 86, 96, 83, 53]
[5, 9, 12, 19, 25, 43, 45, 43, 75, 84, 86, 96, 83, 53]
[5, 9, 12, 19, 25, 43, 43, 45, 75, 84, 86, 96, 83, 53]
43 不小于 43 , 停止对比 , 跳出此次循环
比较第12个数与之前的数
[5, 9, 12, 19, 25, 43, 43, 45, 75, 84, 86, 96, 83, 53]
[5, 9, 12, 19, 25, 43, 43, 45, 75, 84, 86, 83, 96, 53]
[5, 9, 12, 19, 25, 43, 43, 45, 75, 84, 83, 86, 96, 53]
[5, 9, 12, 19, 25, 43, 43, 45, 75, 83, 84, 86, 96, 53]
83 不小于 75 , 停止对比 , 跳出此次循环
比较第13个数与之前的数
[5, 9, 12, 19, 25, 43, 43, 45, 75, 83, 84, 86, 96, 53]
[5, 9, 12, 19, 25, 43, 43, 45, 75, 83, 84, 86, 53, 96]
[5, 9, 12, 19, 25, 43, 43, 45, 75, 83, 84, 53, 86, 96]
[5, 9, 12, 19, 25, 43, 43, 45, 75, 83, 53, 84, 86, 96]
[5, 9, 12, 19, 25, 43, 43, 45, 75, 53, 83, 84, 86, 96]
[5, 9, 12, 19, 25, 43, 43, 45, 53, 75, 83, 84, 86, 96]
53 不小于 45 , 停止对比 , 跳出此次循环
[5, 9, 12, 19, 25, 43, 43, 45, 53, 75, 83, 84, 86, 96]
Process finished with exit code 0