希尔排序是插入排序的优化版,其算法表示如下:
def shell_sort(arr):
if len(arr) == 1:
return arr
gap = round(len(arr) / 2)
while gap > 0:
# insert sort
for i in range(gap, len(arr)):
temp = arr[i]
j = i - gap
while j >= 0 and arr[j] > temp:
arr[j + gap] = arr[j]
j -= gap
arr[j + gap] = temp
gap = round(gap / 2)
return arr