diff --git a/sorts/comb_sort.py b/sorts/comb_sort.py index 72caeb9c7350..bbe57234ffe8 100644 --- a/sorts/comb_sort.py +++ b/sorts/comb_sort.py @@ -18,8 +18,14 @@ python comb_sort.py """ +from typing import Protocol -def comb_sort(data: list) -> list: + +class Comparable(Protocol): + def __lt__(self, other: object, /) -> bool: ... + + +def comb_sort[T: Comparable](data: list[T]) -> list[T]: """Pure implementation of comb sort algorithm in Python :param data: mutable collection with comparable items :return: the same collection in ascending order @@ -32,6 +38,11 @@ def comb_sort(data: list) -> list: [-15, -7, 0, 2, 3, 8, 45, 99] >>> comb_sort([2, 0, 3, 4, 5, 6, 1]) [0, 1, 2, 3, 4, 5, 6] + + >>> comb_sort(["d", "a", "c", "b"]) + ['a', 'b', 'c', 'd'] + >>> comb_sort([2.5, -1.0, 0.0]) + [-1.0, 0.0, 2.5] """ shrink_factor = 1.3 gap = len(data) diff --git a/tests/test_sorts.py b/tests/test_sorts.py index caa4b31cac81..d6351c789353 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -96,6 +96,7 @@ def test_sort_matches_builtin(sort, case): bubble_sort_iterative, bubble_sort_recursive, insertion_sort, + comb_sort, ], ids=lambda f: f.__name__, )