-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.py
More file actions
27 lines (23 loc) · 818 Bytes
/
Copy pathShellSort.py
File metadata and controls
27 lines (23 loc) · 818 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
27
# 谢尔排序
def shellSort(alist):
# 间隔设定
sublistcount = len(alist) // 2
while sublistcount > 0:
# 子列表排序
for startpos in range(sublistcount):
# 带间隔的插入排序
gapinsertionSort(alist, startpos, sublistcount)
print("After increment of size", sublistcount, "The list is", alist)
sublistcount = sublistcount // 2
return alist
def gapinsertionSort(alist, start, gap):
for i in range(start + gap, len(alist), gap):
currentvalue = alist[i]
pos = i
while pos >= gap and alist[pos - gap] > currentvalue:
alist[pos] = alist[pos - gap]
pos = pos - gap
alist[pos] = currentvalue
lista = [17, 26, 93, 44, 77, 31, 54, 55, 20]
lista1 = shellSort(lista)
print(lista1)