forked from paradoxpj/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_sort.py
More file actions
33 lines (25 loc) · 721 Bytes
/
shell_sort.py
File metadata and controls
33 lines (25 loc) · 721 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
def shell_sort(arr):
"""
Fuction to sort using Shell Sort
<https://en.wikipedia.org/wiki/Shellsort>.
:param arr: A list of element to sort
"""
gap = int((len(arr)/2))
while gap > 0:
for i in range(gap, len(arr)):
temp = arr[i]
j = i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j-gap]
j -= gap
arr[j] = temp
gap /= 2
gap = int(gap)
return arr
def main():
arr = [15, 12, 36, 63, 96]
sorted_arr = shell_sort(arr)
print('Sorted element using Shell Sort: {}'.format(
' '.join(map(str, shell_sort(arr)))))
if __name__ == '__main__':
main()