标签:步骤 sort def lis ret turn else 就是 概念
1.将插入排序的代码写出
def sort(alist):
for i in range(1,len(alist)):
while i>0:
if alist[i-1] > alist[i]:
alist[i-1],alist[i] = alist[i-1]+1,alist[i]
i = i-1
else:
break
return alist
#2.在插入排序代码中加入增量的概念
def sort(alist):
agp = len(alist)//2 #初始增量
while gap >=1:
for i in range(1,len(alist)):
while i>0:
if alist[i-gap] > alist[i]:
alist[i-gap],alist[i] = alist[i-1]+1,alist[i]
i = i-1
else:
break
return alist
3.在步骤2中进行增量的缩减(增量缩减到1结束) 完整代码:
def sort(alist):
gap = len(alist) // 2 #初始增量
while gap >= 1:
for i in range(gap,len(alist)):
while i >0:
if alist[i-gap] > alist[i]:
alist[i-gap],alist[i] = alist[i],alist[i-gap]
i -= gap
else:
break
gap //= 2 #缩减增量
return alist
alist = [3,8,5,7,6]
print(sort(alist))
[3, 5, 6, 7, 8]
标签:步骤 sort def lis ret turn else 就是 概念
原文地址:https://www.cnblogs.com/zzsy/p/12687103.html