diff --git a/sorts/exchange_sort.py b/sorts/exchange_sort.py index 1ce78a9dc0cb..49a84ca4dfce 100644 --- a/sorts/exchange_sort.py +++ b/sorts/exchange_sort.py @@ -1,4 +1,11 @@ -def exchange_sort(numbers: list[int]) -> list[int]: +from typing import Any, Protocol + + +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +def exchange_sort[T: Comparable](numbers: list[T]) -> list[T]: """ Uses exchange sort to sort a list of numbers. Source: https://en.wikipedia.org/wiki/Sorting_algorithm#Exchange_sort @@ -12,6 +19,14 @@ def exchange_sort(numbers: list[int]) -> list[int]: [-2, 0, 3, 5, 10] >>> exchange_sort([]) [] + >>> exchange_sort(["c", "a", "b"]) + ['a', 'b', 'c'] + >>> exchange_sort([2.5, -1, 0.0]) + [-1, 0.0, 2.5] + >>> exchange_sort([1, "a"]) + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' """ numbers_length = len(numbers) for i in range(numbers_length): diff --git a/tests/test_sorts.py b/tests/test_sorts.py index 764c3eed2861..2c9b79aa4bfe 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -117,6 +117,7 @@ def test_sort_matches_builtin(sort, case) -> None: circle_sort, cocktail_shaker_sort, comb_sort, + exchange_sort, gnome_sort, insertion_sort, merge_sort,