From 9c225f701bc194174f895759c3694b532cb3e529 Mon Sep 17 00:00:00 2001 From: Harsh Raj Singhania <40535627+HarshRajSinghania@users.noreply.github.com> Date: Wed, 9 Sep 2026 23:39:11 +0530 Subject: [PATCH 1/3] sorts: type selection sort for comparable items --- sorts/selection_sort.py | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/sorts/selection_sort.py b/sorts/selection_sort.py index a0261e9db030..7b0f0c439706 100644 --- a/sorts/selection_sort.py +++ b/sorts/selection_sort.py @@ -1,4 +1,15 @@ -def selection_sort(collection: list[int]) -> list[int]: +from collections.abc import MutableSequence +from typing import Any, Protocol, TypeVar + + +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +T = TypeVar("T", bound=Comparable) + + +def selection_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequence[T]: """ Sorts a list in ascending order using the selection sort algorithm. @@ -9,8 +20,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]) @@ -28,25 +39,12 @@ def selection_sort(collection: list[int]) -> list[int]: >>> selection_sort([5, 4, 3, 2, 1]) [1, 2, 3, 4, 5] - >>> selection_sort([1, 2, 3, 4, 5]) - [1, 2, 3, 4, 5] - - >>> selection_sort([3, 3, 3, 3]) - [3, 3, 3, 3] - - >>> selection_sort([0]) - [0] + >>> selection_sort(["d", "a", "c", "b"]) + ['a', 'b', 'c', 'd'] - >>> selection_sort([2, -3, 0, 5, -1]) - [-3, -1, 0, 2, 5] - - >>> selection_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2]) - True - - >>> selection_sort([-2, -5, -45]) == sorted([-2, -5, -45]) - True + >>> 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 From e9e0b8bfc91ae1dc81f561079f5ed172204cd017 Mon Sep 17 00:00:00 2001 From: Harsh Raj Singhania <40535627+HarshRajSinghania@users.noreply.github.com> Date: Wed, 9 Sep 2026 23:39:32 +0530 Subject: [PATCH 2/3] style: remove unused TypeVar import --- sorts/selection_sort.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sorts/selection_sort.py b/sorts/selection_sort.py index 7b0f0c439706..a8934d6c0566 100644 --- a/sorts/selection_sort.py +++ b/sorts/selection_sort.py @@ -1,14 +1,11 @@ from collections.abc import MutableSequence -from typing import Any, Protocol, TypeVar +from typing import Any, Protocol class Comparable(Protocol): def __lt__(self, other: Any, /) -> bool: ... -T = TypeVar("T", bound=Comparable) - - def selection_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequence[T]: """ Sorts a list in ascending order using the selection sort algorithm. From fd6129cfe50bd34c6cf8003ecdf2be7c3770303e Mon Sep 17 00:00:00 2001 From: Harsh Raj Singhania <40535627+HarshRajSinghania@users.noreply.github.com> Date: Wed, 9 Sep 2026 23:50:02 +0530 Subject: [PATCH 3/3] test: restore selection sort doctests --- sorts/selection_sort.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/sorts/selection_sort.py b/sorts/selection_sort.py index a8934d6c0566..33c60df85db8 100644 --- a/sorts/selection_sort.py +++ b/sorts/selection_sort.py @@ -36,6 +36,24 @@ def selection_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequ >>> selection_sort([5, 4, 3, 2, 1]) [1, 2, 3, 4, 5] + >>> selection_sort([1, 2, 3, 4, 5]) + [1, 2, 3, 4, 5] + + >>> selection_sort([3, 3, 3, 3]) + [3, 3, 3, 3] + + >>> selection_sort([0]) + [0] + + >>> selection_sort([2, -3, 0, 5, -1]) + [-3, -1, 0, 2, 5] + + >>> selection_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2]) + True + + >>> selection_sort([-2, -5, -45]) == sorted([-2, -5, -45]) + True + >>> selection_sort(["d", "a", "c", "b"]) ['a', 'b', 'c', 'd']