# 希尔排序
def shell_sort(input_list):
# 希尔排序:三重循环,依次插入,直接插入法的优化版
l = input_list # 简化参数名
gaps = [701, 301, 132, 57, 23, 10, 4, 1] # Marcin Ciura's gap sequence
for gap in gaps:
for i in range(gap, len(l)):
insert_value = l[i] # 终止条件
j = i
while j >= gap and l[j - gap] > insert_value:
l[j] = l[j - gap]
j -= gap
if j != i:
l[j] = insert_value # 循环终止,插入值
return l
'''# 另外一种写法
def shell_sort(input_list):
# 希尔排序:三重循环,依次插入,直接插入法的优化版
l = input_list # 简化参数名
gap = len(l) // 2 # 长度取半
while gap > 0:
for i in range(gap, len(l)):
insert_value = l[i] # 终止条件
j = i
while j >= gap and l[j - gap] > insert_value:
l[j] = l[j - gap]
j -= gap
if j != i:
l[j] = insert_value # 循环终止,插入值
gap //= 2
return l
'''
if __name__ == '__main__':
test = [4,3,2,1]
print(shell_sort(test))