Skip to content

sorts: type intro sort for comparable items - #15403

Open
Felix-ming wants to merge 5 commits into
TheAlgorithms:masterfrom
Felix-ming:type/intro-sort-comparable
Open

Felix-ming wants to merge 5 commits into
TheAlgorithms:masterfrom
Felix-ming:type/intro-sort-comparable

Conversation

@Felix-ming

Copy link
Copy Markdown

Describe your change

  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?

Type IntroSort and its helpers for mutually comparable items. This adds coverage for numeric and string inputs and verifies that mixed incomparable values raise TypeError.

Part of #15234

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work.
  • I understand automated tests must pass before merge.
  • This PR changes one algorithm implementation.
  • All new Python files are in an existing directory.
  • All names follow Python naming conventions.
  • All function parameters and return values are annotated.
  • All doctests pass under the supported Python version.

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass labels Sep 22, 2026
Comment thread tests/test_intro_sort.py Outdated
@cclauss

cclauss commented Sep 23, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev, please review.

@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Sep 23, 2026
@cclauss

cclauss commented Sep 23, 2026

Copy link
Copy Markdown
Member

Please add to tests/test_sorts.py the line from sorts.intro_sort import sort as intro_sort and then add intro_sort to the SORTS tuple and @pytest.mark.parametrize list.

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

LGTM — the PEP 695 generic conversion is correct. I ran the doctests on Python 3.12 (41/41 pass) and spot-checked further:

  • The median_of_3 rewrite to use only < (to match the Comparable protocol's single __lt__) is behavior-preserving: a > bb < a, so both != branches are logically identical to the originals. Verified equal on 3000 random 3-element inputs.
  • 2500 random int/str arrays sort identically to sorted().
  • The new sort([1, 'a']) doctest raises TypeError as expected.

One note for @Felix-ming: per cclauss's comment, wiring intro_sort into the existing tests/test_sorts.py SORTS tuple (rather than the standalone tests/test_intro_sort.py) keeps the suite consistent. Otherwise this is clean.

@cclauss cclauss added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Sep 23, 2026

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 changes A maintainer has requested changes to this PR enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants