forked from codebasics/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
38 lines (35 loc) · 892 Bytes
/
shell_sort.py
File metadata and controls
38 lines (35 loc) · 892 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
30
31
32
33
34
35
36
37
38
def shell_sort(arr):
size = len(arr)
gap = size//2
while gap > 0:
for i in range(gap,size):
anchor = arr[i]
j = i
while j>=gap and arr[j-gap]>anchor:
arr[j] = arr[j-gap]
j -= gap
arr[j] = anchor
gap = gap // 2
def foo(arr):
size = len(arr)
gap = size // 2
gap = 3
for i in range(gap, size):
anchor = arr[i]
j = i
while j>=gap and arr[j-gap]>anchor:
arr[j] = arr[j-gap]
j -= gap
arr[j] = anchor
if __name__ == '__main__':
tests = [
[89, 78, 61, 53, 23, 21, 17, 12, 9, 7, 6, 2, 1],
[],
[1,5,8,9],
[234,3,1,56,34,12,9,12,1300],
[5]
]
elements = [89,78,61,53,23,21,17,12,9,7,6,2,1]
for elements in tests:
shell_sort(elements)
print(elements)