From 670d41d9c60bae0dea4877b56e082c3cf0e7a1ab Mon Sep 17 00:00:00 2001 From: Pradhyut21 Date: Mon, 14 Sep 2026 19:35:41 +0530 Subject: [PATCH 1/4] sorts: make bogo_sort work with any Comparable, not just int Use a Comparable Protocol + TypeVar bound so that bogo_sort correctly expresses it can sort any orderable type, not just integers. Adds string and float doctest examples. Refactors is_sorted() as an inner helper with proper typing. Part of #15234 --- sorts/bogo_sort.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/sorts/bogo_sort.py b/sorts/bogo_sort.py index 70785140ee5c..0e4af624621a 100644 --- a/sorts/bogo_sort.py +++ b/sorts/bogo_sort.py @@ -14,13 +14,23 @@ """ import random +from typing import Any, Protocol, TypeVar -def bogo_sort(collection: list) -> list: - """Pure implementation of the bogosort algorithm in Python +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +T = TypeVar("T", bound=Comparable) + + +def bogo_sort(collection: list[T]) -> list[T]: + """Pure implementation of the bogosort algorithm in Python. + :param collection: some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending + Examples: >>> bogo_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] @@ -28,11 +38,16 @@ def bogo_sort(collection: list) -> list: [] >>> bogo_sort([-2, -5, -45]) [-45, -5, -2] + >>> bogo_sort(["c", "a", "b"]) + ['a', 'b', 'c'] + >>> bogo_sort([2.5, -1.0, 0.0]) + [-1.0, 0.0, 2.5] + >>> bogo_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2]) + True """ - - def is_sorted(collection: list) -> bool: + def is_sorted(collection: list[T]) -> bool: for i in range(len(collection) - 1): - if collection[i] > collection[i + 1]: + if collection[i + 1] < collection[i]: return False return True From f7c6a159b78e7450c4302f7bce439688d208ac3e Mon Sep 17 00:00:00 2001 From: Pradhyut21 Date: Mon, 14 Sep 2026 20:39:54 +0530 Subject: [PATCH 2/4] fix: use PEP 695 type params and update formatting --- sorts/shell_sort.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index b65609c974b7..6658112af9dc 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -2,28 +2,43 @@ https://en.wikipedia.org/wiki/Shellsort#Pseudocode """ +from typing import Any, Protocol + + +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +def shell_sort[T: Comparable](collection: list[T]) -> list[T]: + """Pure implementation of shell sort algorithm in Python. -def shell_sort(collection: list[int]) -> list[int]: - """Pure implementation of shell sort algorithm in Python :param collection: Some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending + Examples: >>> shell_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >>> shell_sort([]) [] >>> shell_sort([-2, -5, -45]) [-45, -5, -2] + >>> shell_sort(["c", "a", "b"]) + ['a', 'b', 'c'] + >>> shell_sort([2.5, -1.0, 0.0]) + [-1.0, 0.0, 2.5] + >>> shell_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2]) + True + >>> shell_sort(["c", "a", "b"]) == sorted(["c", "a", "b"]) + True """ # Marcin Ciura's gap sequence - gaps = [701, 301, 132, 57, 23, 10, 4, 1] for gap in gaps: for i in range(gap, len(collection)): insert_value = collection[i] j = i - while j >= gap and collection[j - gap] > insert_value: + while j >= gap and insert_value < collection[j - gap]: collection[j] = collection[j - gap] j -= gap if j != i: From 3fefc7b432ca067f432392ce7206cfdb700b0029 Mon Sep 17 00:00:00 2001 From: Pradhyut21 Date: Mon, 14 Sep 2026 20:39:58 +0530 Subject: [PATCH 3/4] fix: use PEP 695 type params --- sorts/quick_sort.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 374d52e75c81..e5e2254050a9 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -11,9 +11,14 @@ from __future__ import annotations from random import randrange +from typing import Any, Protocol -def quick_sort(collection: list) -> list: +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +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 @@ -26,27 +31,26 @@ 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 3bc37c7aebbdf18a3a2805b19001b34c4f53c74d Mon Sep 17 00:00:00 2001 From: Pradhyut21 Date: Mon, 14 Sep 2026 20:40:01 +0530 Subject: [PATCH 4/4] fix: use PEP 695 type params, fix formatting and is_sorted logic --- sorts/bogo_sort.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/sorts/bogo_sort.py b/sorts/bogo_sort.py index 0e4af624621a..009e28055a04 100644 --- a/sorts/bogo_sort.py +++ b/sorts/bogo_sort.py @@ -14,17 +14,14 @@ """ import random -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 bogo_sort(collection: list[T]) -> list[T]: +def bogo_sort[T: Comparable](collection: list[T]) -> list[T]: """Pure implementation of the bogosort algorithm in Python. :param collection: some mutable ordered collection with heterogeneous @@ -45,11 +42,11 @@ def bogo_sort(collection: list[T]) -> list[T]: >>> bogo_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2]) True """ + def is_sorted(collection: list[T]) -> bool: - for i in range(len(collection) - 1): - if collection[i + 1] < collection[i]: - return False - return True + return all( + collection[i] <= collection[i + 1] for i in range(len(collection) - 1) + ) while not is_sorted(collection): random.shuffle(collection)