##希尔排序
def sort(arr):
h = 1
size = len(arr)
while h < size // 3:
h = 3 * h + 1
while h >= 1:
for i in range(h, size):
j = i
while j >= h and arr[j] < arr[j-h]:
tmp = arr[j-h]
arr[j-h] = arr[j]
arr[j] = tmp
j -= h
h = h // 3
return arr
##############验证#############
import numpy as np
len2 = 10
###生成随机整数列表
arr = np.random.randint(len2 * 10, size=len2)
print('排序前:')
print(arr)
sort(arr)
print('排序后:')
print(arr)
网友评论