- Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathShellSort.py
More file actions
Latest commit
21 lines (18 loc) · 680 Bytes
/
Copy pathShellSort.py
File metadata and controls
21 lines (18 loc) · 680 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# python实现希尔排序
defshellSort(alist):
sublistcount=len(alist)//2
whilesublistcount>0:
forstartpositioninrange(sublistcount):
gapInsertionSort(alist, startposition, sublistcount)
sublistcount=sublistcount//2
returnalist
defgapInsertionSort(alist, start, gap):
foriinrange(start+gap, len(alist), gap):
currentValue=alist[i]
position=i
whileposition>=gapandalist[position-gap] >currentValue:
alist[position] =alist[position-gap]
position=position-gap
alist[position] =currentValue
alist= [54,26,93,17,77,31,44,55,20]
print(shellSort(alist))