Skip to content

typing: bound recursive quick sort to Comparable protocol - #15473

Closed
Silverbullets1 wants to merge 3 commits into
TheAlgorithms:masterfrom
Silverbullets1:typing/recursive-quick-15234
Closed

Silverbullets1 wants to merge 3 commits into
TheAlgorithms:masterfrom
Silverbullets1:typing/recursive-quick-15234

Conversation

@Silverbullets1

Copy link
Copy Markdown
Contributor

Describe your change

  • Add or change doctests?

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Part of #15234 (the recursive_quick_sort.py checkbox).

This PR types the recursive quick sort for any comparable items:

  • Replaced the implicit Any element type with the Comparable Protocol + TypeVar bound (quick_sort[T: Comparable](data: list[T]) -> list[T]), matching the pattern already checked in for other sorts in this issue (e.g. bogo_sort).
  • Partitioning now relies only on __lt__: not data[0] < e for the left partition and data[0] < e for the right, replacing the former mix of <= and >.
  • New doctests: strings sort, floats and ints mix sorts, and quick_sort([1, "a"]) raises TypeError.
  • recursive_quick_sort joins the shared battery in tests/test_sorts.py: it is added to both test_sort_matches_builtin (all existing cases) and test_sort_rejects_non_comparable_items, plus a dedicated test_recursive_quick_sort_mixed_types.

Verification:

$ uvx ruff check sorts/recursive_quick_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_quick_sort.py -q --no-header
1 passed in 0.02s

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

- Any-era hints -> Comparable Protocol + TypeVar bound, __lt__-only partitioning
- recursive_quick_sort added to tests/test_sorts.py battery + reject test
- doctests: strings, floats+ints mix, mixed [1, 'a'] raises TypeError

Part of TheAlgorithms#15234
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 1, 2026
@algorithms-keeper algorithms-keeper Bot added tests are failing Do not merge until tests pass labels Oct 1, 2026
@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Oct 1, 2026
@cclauss cclauss closed this Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants