Skip to content

Commit abb2523

Browse files
authored
Merge pull request keon#90 from ss18/sort/comb-sort
Implemented Comb Sort
2 parents 76a1d9d + 3aaeadc commit abb2523

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Minimal and clean example implementations of data structures and algorithms in P
130130
- [randomized_set](set/randomized_set.py)
131131
- [sort](sort)
132132
- [bubble_sort](sort/bubble_sort.py)
133+
- [comb_sort](sort/comb_sort.py)
133134
- [counting_sort](sort/counting_sort.py)
134135
- [heap_sort](sort/heap_sort.py)
135136
- [insertion_sort](sort/insertion_sort.py)

sort/bubble_sort.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
https://en.wikipedia.org/wiki/Bubble_sort
44
5-
Complexity: O(N^2)
5+
Worst-case performance: O(N^2)
66
77
"""
88

sort/comb_sort.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
3+
https://en.wikipedia.org/wiki/Comb_sort
4+
5+
Worst-case performance: O(N^2)
6+
7+
"""
8+
9+
from math import floor
10+
11+
12+
def comb_sort(arr):
13+
14+
def swap(i, j):
15+
arr[i], arr[j] = arr[j], arr[i]
16+
17+
n = len(arr)
18+
gap = n
19+
shrink = 1.3
20+
sorted = False
21+
while not sorted:
22+
gap = int(floor(gap/shrink))
23+
if gap > 1:
24+
sorted = False
25+
else:
26+
gap = 1
27+
sorted = True
28+
29+
i = 0
30+
while i + gap < n:
31+
if arr[i] > arr[i + gap]:
32+
swap(i, i + gap)
33+
sorted = False
34+
i = i + 1
35+
36+
37+
array = [1, 5, 65, 23, 57, 1232, -1, -5, -2, 242, 100,
38+
4, 423, 2, 564, 9, 0, 10, 43, 64, 32, 1, 999]
39+
print(array)
40+
comb_sort(array)
41+
print(array)

0 commit comments

Comments
 (0)