From 82cb9c7fd426a42ab16dd6a26b20feda82794af3 Mon Sep 17 00:00:00 2001 From: Pradhyut21 Date: Mon, 14 Sep 2026 19:35:07 +0530 Subject: [PATCH 1/2] sorts: make quick_sort work with any Comparable, not just int Use a Comparable Protocol + TypeVar bound so that quick_sort correctly expresses it can sort any orderable type, not just integers. Simplifies partitioning to cleaner lesser/equal/greater lists and adds string + float doctest examples. Part of #15234 --- sorts/quick_sort.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 374d52e75c81..27962c704843 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -11,9 +11,17 @@ from __future__ import annotations from random import randrange +from typing import Any, Protocol, TypeVar -def quick_sort(collection: list) -> list: +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +T = TypeVar("T", bound=Comparable) + + +def quick_sort(collection: list[T]) -> list[T]: """A pure Python implementation of quicksort algorithm. :param collection: a mutable collection of comparable items @@ -26,27 +34,27 @@ def quick_sort(collection: list) -> list: [] >>> quick_sort([-2, 5, 0, -45]) [-45, -2, 0, 5] + >>> quick_sort(["z", "a", "m", "b"]) + ['a', 'b', 'm', 'z'] + >>> quick_sort([3.14, -1.0, 2.71]) + [-1.0, 2.71, 3.14] + >>> quick_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2]) + True + >>> quick_sort(["z", "a", "m"]) == sorted(["z", "a", "m"]) + True """ # Base case: if the collection has 0 or 1 elements, it is already sorted if len(collection) < 2: return collection - - # Randomly select a pivot index and remove the pivot element from the collection pivot_index = randrange(len(collection)) - pivot = collection.pop(pivot_index) - - # Partition the remaining elements into two groups: lesser or equal, and greater - lesser = [item for item in collection if item <= pivot] + pivot = collection[pivot_index] + lesser = [item for item in collection if item < pivot] + equal = [item for item in collection if item == pivot] greater = [item for item in collection if item > pivot] - - # Recursively sort the lesser and greater groups, and combine with the pivot - return [*quick_sort(lesser), pivot, *quick_sort(greater)] + return [*quick_sort(lesser), *equal, *quick_sort(greater)] if __name__ == "__main__": - # Get user input and convert it into a list of integers user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] - - # Print the result of sorting the user-provided list print(quick_sort(unsorted)) From 70006539a0ef9d7d8ccbaa22331ca880e8939ddc Mon Sep 17 00:00:00 2001 From: Pradhyut21 Date: Mon, 14 Sep 2026 20:40:35 +0530 Subject: [PATCH 2/2] fix: use PEP 695 type params, remove TypeVar import --- sorts/quick_sort.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 27962c704843..e5e2254050a9 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -11,17 +11,14 @@ from __future__ import annotations from random import randrange -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 quick_sort(collection: list[T]) -> list[T]: +def quick_sort[T: Comparable](collection: list[T]) -> list[T]: """A pure Python implementation of quicksort algorithm. :param collection: a mutable collection of comparable items @@ -43,7 +40,6 @@ def quick_sort(collection: list[T]) -> list[T]: >>> quick_sort(["z", "a", "m"]) == sorted(["z", "a", "m"]) True """ - # Base case: if the collection has 0 or 1 elements, it is already sorted if len(collection) < 2: return collection pivot_index = randrange(len(collection))