forked from apachecn/apachecn-algo-zh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.py
More file actions
26 lines (23 loc) · 748 Bytes
/
ShellSort.py
File metadata and controls
26 lines (23 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# coding: utf8
from __future__ import print_function
def insert_sort(l, start, increment):
for i in range(start+increment, len(l), increment):
for j in range(start, len(l[:i]), increment):
if l[i] < l[j]:
l[i], l[j] = l[j], l[i]
print(increment, '--',l)
return l
def shell_sort(l, increment):
# 依次进行分层
while increment:
# 每一层,都进行n次插入排序
for i in range(0, increment):
insert_sort(l, i, increment)
increment -= 1
return l
if __name__ == "__main__":
l = [5, 2, 9, 8, 1, 10, 3, 4, 7]
increment = len(l)/3+1 if len(l)%3 else len(l)/3
print("开始", l)
l = shell_sort(l, increment)
print("结束", l)