forked from NITSkmOS/Algorithms
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_sort.py
More file actions
Latest commit
33 lines (25 loc) · 721 Bytes
/
Copy pathshell_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
defshell_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))
whilegap>0:
foriinrange(gap, len(arr)):
temp=arr[i]
j=i
whilej>=gapandarr[j-gap] >temp:
arr[j] =arr[j-gap]
j-=gap
arr[j] =temp
gap/=2
gap=int(gap)
returnarr
defmain():
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()