代码:
#! /usr/bin/env python
#coding=utf-8
import random
def shell_sort_pass(lst,left,delta):
for i in range(left+delta,len(lst),delta):
pivot = lst[i]
while (i > left and lst[i-delta] > pivot):
lst[i] = lst[i-delta]
i = i - delta
lst[i] = pivot
def shell_sort(lst):
sublist_cnt = len(lst) >> 1
while sublist_cnt > 0:
for i in range(sublist_cnt):
shell_sort_pass(lst,i,sublist_cnt)
sublist_cnt = sublist_cnt >> 1
if __name__ == '__main__':
lst = [random.randint(0,20) for i in range(10)]
lst2 = lst[:]
lst2.sort()
print(lst)
print(lst2)
shell_sort(lst)
print(lst)
>>>
[6, 17, 20, 14, 6, 7, 18, 7, 18, 3]
[3, 6, 6, 7, 7, 14, 17, 18, 18, 20]
[3, 6, 6, 7, 7, 14, 17, 18, 18, 20]