Skip to content

Commit 29c12a3

Browse files
sorts: type comb sort for comparable items
1 parent 927c7d3 commit 29c12a3

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎sorts/comb_sort.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@
1818
python comb_sort.py
1919
"""
2020

21+
from typing import Protocol
2122

22-
def comb_sort(data: list) -> list:
23+
24+
class Comparable(Protocol):
25+
def __lt__(self, other: object, /) -> bool: ...
26+
27+
28+
def comb_sort[T: Comparable](data: list[T]) -> list[T]:
2329
"""Pure implementation of comb sort algorithm in Python
2430
:param data: mutable collection with comparable items
2531
:return: the same collection in ascending order
@@ -32,7 +38,12 @@ def comb_sort(data: list) -> list:
3238
[-15, -7, 0, 2, 3, 8, 45, 99]
3339
>>> comb_sort([2, 0, 3, 4, 5, 6, 1])
3440
[0, 1, 2, 3, 4, 5, 6]
35-
"""
41+
42+
>>> comb_sort(["d", "a", "c", "b"])
43+
['a', 'b', 'c', 'd']
44+
>>> comb_sort([2.5, -1.0, 0.0])
45+
[-1.0, 0.0, 2.5]
46+
"""
3647
shrink_factor = 1.3
3748
gap = len(data)
3849
completed = False

‎tests/test_sorts.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def test_sort_matches_builtin(sort, case):
9696
bubble_sort_iterative,
9797
bubble_sort_recursive,
9898
insertion_sort,
99+
comb_sort,
99100
],
100101
ids=lambda f: f.__name__,
101102
)

0 commit comments

Comments
 (0)