Skip to content

feat(sorts): make recursive quick sort generic over comparable items - #15414

Open
HasanGenis wants to merge 4 commits into
TheAlgorithms:masterfrom
HasanGenis:feature/recursive-quick-sort-comparable-typing
Open

HasanGenis wants to merge 4 commits into
TheAlgorithms:masterfrom
HasanGenis:feature/recursive-quick-sort-comparable-typing

Conversation

@HasanGenis

Copy link
Copy Markdown

Changes
Adds a Comparable Protocol + TypeVar bound to recursive_quick_sort.py's
quick_sort() so it is typed list[T] instead of a bare list, matching the
pattern already used in insertion_sort.py. Adds a doctest asserting TypeError
on non-comparable mixed-type input (quick_sort([1, "a"])). Registers it (as
recursive_quick_sort, aliased on import to avoid colliding with the unrelated
sorts.quick_sort.quick_sort) in the shared SORTS battery and in
test_sort_rejects_non_comparable_items.

Part of #15234

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?
    -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

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".

Add a Comparable Protocol + TypeVar bound to recursive_quick_sort's quick_sort() so it is typed list[T] instead of a bare list, matching the pattern in insertion_sort.py. Adds a doctest asserting TypeError on non-comparable mixed-type input and registers it (aliased to recursive_quick_sort to avoid colliding with sorts.quick_sort.quick_sort) in the shared test battery and in test_sort_rejects_non_comparable_items.

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 Sep 23, 2026
@cclauss

cclauss commented Sep 23, 2026

Copy link
Copy Markdown
Member

ON HOLD: Our focus is on merging or closing old pull requests before October 1st.

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @HasanGenis! Doctests pass. Two small things to make this consistent with the Comparable bound:

  1. The declared protocol only guarantees __lt__, but the body uses <= and >:
*quick_sort([e for e in data[1:] if e <= data[0]]),
data[0],
*quick_sort([e for e in data[1:] if e > data[0]]),

A __lt__-only type has neither __le__ nor __gt__. You can keep the partition correct using only <:

pivot = data[0]
rest = data[1:]
left = [e for e in rest if e < pivot]
right = [e for e in rest if not (e < pivot)]
return [*quick_sort(left), pivot, *quick_sort(right)]
  1. T = TypeVar("T", bound=Comparable) (and the TypeVar import) are now dead code — the PEP 695 def quick_sort[T: Comparable] syntax defines its own T, so the module-level TypeVar is unused and can be dropped.

Comment thread sorts/recursive_quick_sort.py Outdated
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 24, 2026
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Sep 24, 2026
@cclauss

cclauss commented Sep 24, 2026

Copy link
Copy Markdown
Member

I did this change, so please do git pull.

T = TypeVar("T", bound=Comparable) (and the TypeVar import) are now dead code

@cclauss
cclauss self-requested a review September 24, 2026 14:43
@cclauss

cclauss commented Sep 25, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev can you please add a GitHub suggestion to fix point 1 above #15414 (review)

This branch has not been deployed

No deployments
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 on hold

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants