forked from OliverIgnetik/Data-Structures-Algorithms-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_sort.py
More file actions
29 lines (22 loc) · 788 Bytes
/
Copy pathshell_sort.py
File metadata and controls
29 lines (22 loc) · 788 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
28
29
def shell_sort(arr):
sublistcount = len(arr)//2
# While we still have sub lists
while sublistcount > 0:
for start in range(sublistcount):
# Use a gap insertion
gap_insertion_sort(arr, start, sublistcount)
sublistcount = sublistcount // 2
def gap_insertion_sort(arr, start, gap):
for i in range(start+gap, len(arr), gap):
currentvalue = arr[i]
position = i
# Using the Gap
while position >= start+gap and arr[position-gap] > currentvalue:
# swap the elements at these indexes
arr[position] = arr[position-gap]
position = position-gap
# Set current value
arr[position] = currentvalue
arr = [7, 3, 4, 10, 9, 8, 1, 6]
shell_sort(arr)
print(arr)