https://blog.csdn.net/qq_39207948/article/details/80006224

def shell_sort(lists):
    count = len(lists)
    step = 2
    group = count // step
    while group > 0:
        for i in range(0, group):
            j = i + group
            while j < count:
                k = j - group
                key = lists[j]
                while k >= 0:
                    if lists[k] > key:
                        lists[k+group] = lists[k]
                        lists[k] = key
                    k -= group
                j += group
        group //= step
    return lists

if __name__ == '__main__':
    list = [3, 4, 2, 8, 9, 5, 1]
    print("排序前:")
    for ele in list:
        print(ele, end=" ")
    print("\n排序后:")
    for ele in shell_sort(list):
        print(ele, end=" ")

 

posted on 2019-08-08 16:55  蔡狗八  阅读(129)  评论(0)    收藏  举报