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
36 lines (26 loc) · 764 Bytes
/
comb_sort.py
File metadata and controls
36 lines (26 loc) · 764 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
"""
Comb Sort
---------
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):
"""
Takes a list of integers and sorts them in ascending order. This sorted
list is then returned.
:param seq: A list of integers
:rtype: A list of sorted integers
"""
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