-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell sort.py
More file actions
23 lines (17 loc) · 872 Bytes
/
Shell sort.py
File metadata and controls
23 lines (17 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def shell_sort(list):
gap = len(list)//2 #the gap formula can be change to optimize computation speed
while gap >0:
for i in range(gap):
list=gapInsertionSort(i,list,gap)
gap = gap // 2 #the gap formula can be change to optimize computation speed
return list
def gapInsertionSort(starting_index,list,gap):
for item_index in range(starting_index,len(list),gap):
current_position = item_index
next_position = current_position+gap
while next_position <len(list): #insertion sort between numbers in gap
if list[current_position] > list[next_position]:
list[current_position],list[current_position+gap] = list[current_position+gap],list[current_position]
current_position = next_position
next_position = next_position+gap
return list