diff --git a/sorts/recursive_quick_sort.py b/sorts/recursive_quick_sort.py index c29009aca673..d71e79fd6450 100644 --- a/sorts/recursive_quick_sort.py +++ b/sorts/recursive_quick_sort.py @@ -1,10 +1,22 @@ -def quick_sort(data: list) -> list: +from typing import Any, Protocol + + +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +def quick_sort[T: Comparable](data: list[T]) -> list[T]: """ >>> for data in ([2, 1, 0], [2.2, 1.1, 0], "quick_sort"): ... quick_sort(data) == sorted(data) True True True + + >>> quick_sort([1, "a"]) + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'str' and 'int' """ if len(data) <= 1: return data diff --git a/tests/test_sorts.py b/tests/test_sorts.py index 8338cbb69ca6..c2e20f1aaf55 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -46,6 +46,7 @@ from sorts.quick_sort_3_partition import three_way_radix_quicksort from sorts.recursive_insertion_sort import rec_insertion_sort from sorts.recursive_mergesort_array import merge +from sorts.recursive_quick_sort import quick_sort as recursive_quick_sort from sorts.reverse_selection import reverse_selection_sort from sorts.reversort import reversort from sorts.selection_sort import selection_sort @@ -87,6 +88,7 @@ def test_heap_sort() -> None: pancake_sort, patience_sort, quick_sort, + recursive_quick_sort, reverse_selection_sort, reversort, selection_sort, @@ -165,6 +167,7 @@ def test_rec_insertion_sort(case) -> None: odd_even_transposition, pancake_sort, patience_sort, + recursive_quick_sort, reverse_selection_sort, reversort, selection_sort,