Skip to content

Commit 35ea518

Browse files
authored
Merge pull request #67 from imfarhanAK/main
Create comb_sort.py
2 parents 91cb915 + cd027bf commit 35ea518

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

comb_sort.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
def comb_sort(data: list) -> list:
2+
"""Pure implementation of comb sort algorithm in Python
3+
:param data: mutable collection with comparable items
4+
:return: the same collection in ascending order
5+
Examples:
6+
>>> comb_sort([0, 5, 3, 2, 2])
7+
[0, 2, 2, 3, 5]
8+
>>> comb_sort([])
9+
[]
10+
>>> comb_sort([99, 45, -7, 8, 2, 0, -15, 3])
11+
[-15, -7, 0, 2, 3, 8, 45, 99]
12+
"""
13+
shrink_factor = 1.3
14+
gap = len(data)
15+
completed = False
16+
17+
while not completed:
18+
19+
# Update the gap value for a next comb
20+
gap = int(gap / shrink_factor)
21+
if gap <= 1:
22+
completed = True
23+
24+
index = 0
25+
while index + gap < len(data):
26+
if data[index] > data[index + gap]:
27+
# Swap values
28+
data[index], data[index + gap] = data[index + gap], data[index]
29+
completed = False
30+
index += 1
31+
32+
return data
33+
34+
35+
if __name__ == "__main__":
36+
import doctest
37+
38+
doctest.testmod()
39+
40+
user_input = input("Enter numbers separated by a comma:\n").strip()
41+
unsorted = [int(item) for item in user_input.split(",")]
42+
print(comb_sort(unsorted))

0 commit comments

Comments
 (0)