帮我用python写一个希尔排序
好的,以下是一个 Python 实现的希尔排序算法:
def shell_sort(arr):
n = len(arr)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = arr[i]
j = i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
arr[j] = temp
gap //= 2
# 示例
arr = [64, 25, 12, 22, 11]
shell_sort(arr)
print(arr)
输出结果为:[11, 12, 22, 25, 64]。
希望对你有所帮助!
原文地址: https://www.cveoy.top/t/topic/B8t 著作权归作者所有。请勿转载和采集!