Skip to content

typing: bound recursive insertion and quick sorts to Comparable protocol - #15471

Closed
Silverbullets1 wants to merge 1 commit into
TheAlgorithms:masterfrom
Silverbullets1:typing/recursive-sorts-15234
Closed

Silverbullets1 wants to merge 1 commit into
TheAlgorithms:masterfrom
Silverbullets1:typing/recursive-sorts-15234

Conversation

@Silverbullets1

Copy link
Copy Markdown
Contributor

Part of #15234

This PR covers the recursive_insertion_sort.py and recursive_quick_sort.py checkboxes of the umbrella issue.

Changes

sorts/recursive_quick_sort.py

  • Replaced the implicit Any items with the Comparable Protocol + TypeVar bound pattern (quick_sort[T: Comparable](data: list[T]) -> list[T]).
  • Partitioning now relies only on __lt__ (not data[0] < e / data[0] < e), matching the other checked-in comparison sorts.
  • Added doctests: strings sort, floats+ints mix sorts, and quick_sort([1, "a"]) raises TypeError.

sorts/recursive_insertion_sort.py

  • insert_next was missing its TypeVar bound (Pyright flagged the < operator) — now insert_next[T: Comparable], matching rec_insertion_sort.
  • __lt__(self, other: Any) → __lt__(self, other: object) in the Protocol, per the issue's reference pattern.
  • The order check switched from collection[index - 1] <= collection[index] (which needs __le__) to not collection[index] < collection[index - 1], so the algorithm relies on __lt__ alone.
  • Added doctests: floats+ints mix sorts in place, and rec_insertion_sort([1, "a"], 2) raises TypeError.

tests/test_sorts.py

  • Added recursive_quick_sort to the shared parametrized battery (test_sort_matches_builtin across all existing cases) and to test_sort_rejects_non_comparable_items.
  • New test_recursive_quick_sort_mixed_types: strings/floats succeed, mixed [1, "a"] raises TypeError.

Verification

$ uvx ruff check sorts/recursive_quick_sort.py sorts/recursive_insertion_sort.py tests/test_sorts.py
All checks passed!

$ python -m pytest tests/test_sorts.py -q --no-header
444 passed in 0.37s

$ python -m pytest --doctest-modules sorts/recursive_insertion_sort.py sorts/recursive_quick_sort.py -q --no-header
3 passed in 0.04s

Comments on scope: kept to the two files named in the issue's checklist; no drive-by refactors. The __lt__-only comparison preserves exact behavior for previously-working inputs (equal elements still compare correctly, e.g. [3, 2, 3] doctest unchanged).

Best,
Sam | Lead Engineer
DevilX Digital Labs
https://devilx-solution.vercel.app/

- recursive_quick_sort.py: Any -> Comparable Protocol with TypeVar bound
- recursive_insertion_sort.py: bind insert_next TypeVar, __lt__-only compare
- doctests + test_sorts.py: strings/floats succeed, mixed types raise TypeError

Part of TheAlgorithms#15234
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Oct 1, 2026
@algorithms-keeper

Copy link
Copy Markdown

Closing this pull request as invalid

@Silverbullets1, this pull request is being closed as none of the checkboxes have been marked. It is important that you go through the checklist and mark the ones relevant to this pull request. Please read the Contributing guidelines.

If you're facing any problem on how to mark a checkbox, please read the following instructions:

  • Read a point one at a time and think if it is relevant to the pull request or not.
  • If it is, then mark it by putting a x between the square bracket like so: [x]

NOTE: Only [x] is supported so if you have put any other letter or symbol between the brackets, that will be marked as invalid. If that is the case then please open a new pull request with the appropriate changes.

@algorithms-keeper algorithms-keeper Bot closed this Oct 1, 2026
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant