def shell_sort(alist):
'''希尔排序'''
# gap 按照折半来取
# n = 9
n = len(alist)
# gap = 4
gap = n // 2
# gap变化到0之前,插入算法执行次数
while gap >= 1:
# 插入算法,与普通的插入算法的区别就是gap步长
for j in range(gap, n):
# j = [gap +1, gap+2,...]
i = j
while i > 0:
if alist[i] < alist[i-gap]:
alist[i], alist[i-gap] = alist[i-gap], alist[i]
i -= gap
# 缩短gap步长
gap //= 2
06-10
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)