types(reverse_selection): constrain items to Comparable (#15410)

* types(reverse_selection): constrain items to Comparable

Bind reverse_selection_sort's element type to a Comparable Protocol so the
signature says "a list of items that can be compared with each other"
instead of a bare list, and keep the element type in the return.

reverse_subarray only swaps elements and never compares them, so its
TypeVar stays unbounded.

Also adds doctests for a comparable non-int type (strings, floats) and for
the failure mode: mixing non-comparable items must raise TypeError rather
than silently mis-sort. The test battery picks the sort up for the shared
cases and for the rejection check.

* updating DIRECTORY.md

---------

Co-authored-by: AuroraAeon <auroraeon@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: cclauss <cclauss@users.noreply.github.com>
This commit is contained in:
Thomas Peng
2026-09-24 22:09:11 +02:00
committed by GitHub
co-authored by AuroraAeon Christian Clauss cclauss
parent 1d64d4cda6
commit e49caf57d0
3 changed files with 25 additions and 4 deletions
+1
View File
@@ -276,6 +276,7 @@
* [Permutations](data_structures/arrays/permutations.py)
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
* [Product Sum](data_structures/arrays/product_sum.py)
* [Reverse Array](data_structures/arrays/reverse_array.py)
* [Rotate Array](data_structures/arrays/rotate_array.py)
* [Set Matrix Zeroes](data_structures/arrays/set_matrix_zeroes.py)
* [Sparse Table](data_structures/arrays/sparse_table.py)
+21 -4
View File
@@ -4,14 +4,20 @@ A pure Python implementation of the Reverse Selection Sort algorithm
This algorithm progressively sorts the array by reversing subarrays
For doctests run following command:
python3 -m doctest -v reverse_selection_sort.py
python3 -m doctest -v reverse_selection.py
For manual testing run:
python3 reverse_selection_sort.py
python3 reverse_selection.py
"""
from typing import Any, Protocol
def reverse_subarray(arr: list, start: int, end: int) -> None:
class Comparable(Protocol):
def __lt__(self, other: Any, /) -> bool: ...
def reverse_subarray[T](arr: list[T], start: int, end: int) -> None:
"""
Reverse a subarray in-place.
@@ -41,7 +47,7 @@ def reverse_subarray(arr: list, start: int, end: int) -> None:
end -= 1
def reverse_selection_sort(collection: list) -> list:
def reverse_selection_sort[T: Comparable](collection: list[T]) -> list[T]:
"""
A pure implementation of reverse selection sort algorithm in Python
@@ -64,6 +70,17 @@ def reverse_selection_sort(collection: list) -> list:
>>> reverse_selection_sort([5, 4, 3, 2, 1])
[1, 2, 3, 4, 5]
>>> reverse_selection_sort(["banana", "apple", "cherry"])
['apple', 'banana', 'cherry']
>>> reverse_selection_sort([3.14, 1.5, 2.7])
[1.5, 2.7, 3.14]
>>> reverse_selection_sort([1, "a"]) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: ...
"""
n = len(collection)
for i in range(n - 1):
+3
View File
@@ -46,6 +46,7 @@ from sorts.quick_sort import quick_sort
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.reverse_selection import reverse_selection_sort
from sorts.reversort import reversort
from sorts.selection_sort import selection_sort
from sorts.shell_sort import shell_sort
@@ -86,6 +87,7 @@ SORTS = (
pancake_sort,
patience_sort,
quick_sort,
reverse_selection_sort,
reversort,
selection_sort,
shell_sort,
@@ -163,6 +165,7 @@ def test_rec_insertion_sort(case) -> None:
odd_even_transposition,
pancake_sort,
patience_sort,
reverse_selection_sort,
reversort,
selection_sort,
shrink_shell_sort,