forked from nryoung/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_sort.py
More file actions
34 lines (22 loc) · 703 Bytes
/
Copy pathshell_sort.py
File metadata and controls
34 lines (22 loc) · 703 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
"""
shell_sort.py
Implementation of shell sort on an list and returns a sorted list.
Shell Sort Overview:
------------------------
Comparision sort that sorts far away elements first to sort the list
Time Complexity: O(n**2)
Space Complexity: O(1) Auxiliary
Stable: Yes
Psuedo Code: http://en.wikipedia.org/wiki/Shell_sort
"""
def sort(seq):
gaps = [x for x in range(len(seq) // 2, 0, -1)]
for gap in gaps:
for i in range(gap, len(seq)):
temp = seq[i]
j = i
while j >= gap and seq[j - gap] > temp:
seq[j] = seq[j - gap]
j -= gap
seq[j] = temp
return seq