希尔排序算法的python实现

2022-03-11 00:00:00 算法 排序 希尔
"""
作者:皮蛋编程(https://www.pidancode.com)
创建日期:2022/3/22
功能描述:希尔排序算法的python实现
"""
def shellSort(items):
    inc = int(len(items) / 2)
    while inc:
        for i in range(len(items)):
            j = i
            temp = items[i]
            while j >= inc and items[j - inc] > temp:
                items[j] = items[j - inc]
                j -= inc
            items[j] = temp
        inc = int(inc / 2) if int(inc / 2) else (0 if inc == 1 else 1)
a = [35, -8, 11, 1, 68, 0, 3]
shellSort(a)
print(a)

输出:[-8, 0, 1, 3, 11, 35, 68]
以上代码在python3.9环境测试通过

相关文章