forked from nryoung/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomb_sort.py
More file actions
32 lines (22 loc) · 678 Bytes
/
Copy pathcomb_sort.py
File metadata and controls
32 lines (22 loc) · 678 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
"""
comb_sort.py
Implementation of comb sort on a list and returns a sorted list.
Comb Sort Overview:
-------------------
Improves on bubble sort by using a gap sequence to remove turtles.
Time Complexity: O(n**2)
Space Complexity: O(1) Auxiliary
Stable: Yes
Psuedo code: http://en.wikipedia.org/wiki/Comb_sort
"""
def sort(seq):
gap = len(seq)
swap = True
while gap > 1 or swap:
gap = max(1, int(gap / 1.25))
swap = False
for i in range(len(seq) - gap):
if seq[i] > seq[i + gap]:
seq[i], seq[i + gap] = seq[i + gap], seq[i]
swap = True
return seq