diff --git a/sorts/selection_sort.py b/sorts/selection_sort.py index a0261e9db030..33c60df85db8 100644 --- a/sorts/selection_sort.py +++ b/sorts/selection_sort.py @@ -1,4 +1,12 @@ -def selection_sort(collection: list[int]) -> list[int]: +from collections.abc import MutableSequence +from typing import Any, Protocol + + +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +def selection_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequence[T]: """ Sorts a list in ascending order using the selection sort algorithm. @@ -9,8 +17,8 @@ def selection_sort(collection: list[int]) -> list[int]: Time Complexity: O(n²) in all cases Space Complexity: O(1) - :param collection: A list of comparable items to be sorted. - :return: The same list sorted in ascending order. + :param collection: A mutable sequence of comparable items to be sorted. + :return: The same sequence sorted in ascending order. Examples: >>> selection_sort([0, 5, 3, 2, 2]) @@ -45,8 +53,13 @@ def selection_sort(collection: list[int]) -> list[int]: >>> selection_sort([-2, -5, -45]) == sorted([-2, -5, -45]) True - """ + >>> selection_sort(["d", "a", "c", "b"]) + ['a', 'b', 'c', 'd'] + + >>> selection_sort([3.2, 1.1, 2.4, 0.5]) + [0.5, 1.1, 2.4, 3.2] + """ length = len(collection) for i in range(length - 1): min_index = i