shell排序
def shell_sort(list):
n=len(list)
gap=n//2
while gap > 0:
print([i for i in range(gap,n)],'------------')
for i in range(gap,n):
j=i
while j>=gap and list[j-gap]>list[j]:
list[j-gap],list[j]=list[j],list[j-gap]
j-=gap
gap=gap//2
print(list)
shell_sort([3,2,1,4,5,8,6,11,9,8])
def shell_sort(alist):
n = len(alist)
gap = n // 2
while gap > 0:
print([range(gap, n)])
for i in range(gap, n):
j = i
while j >= gap:
if alist[j] < alist[j - gap]:
alist[j], alist[j - gap] = alist[j - gap], alist[j]
j -= gap
else:
break
gap //= 2
if __name__ == "__main__":
list1 = [5, 6, 7, 4, 9, 11, 20, 1]
shell_sort(list1)
print(list1)
shell排序注意循环条件判断,j大于等于gap