Skip to content

sorts: type bubble sort for any comparable items - #15240

Merged
cclauss merged 4 commits into
TheAlgorithms:masterfrom
HarshRajSinghania:sorts/bubble-sort-comparable
Sep 9, 2026
Merged

sorts: type bubble sort for any comparable items#15240
cclauss merged 4 commits into
TheAlgorithms:masterfrom
HarshRajSinghania:sorts/bubble-sort-comparable

Conversation

@HarshRajSinghania

@HarshRajSinghania HarshRajSinghania commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes: #15241

Make bubble_sort_iterative and bubble_sort_recursive correctly typed and tested for any mutually comparable items, not only list[int].

Motivation

Requested in #15234. One comparison sort per PR.

Implementation

  • Replaced list[Any] with a Comparable protocol and def bubble_sort_*[T: Comparable](collection: list[T]) -> list[T], matching insertion_sort.py.
  • Algorithm body is unchanged (still in-place adjacent swaps using >).
  • Added doctests that expect TypeError on mixed non-comparable input ([1, "a"]).
  • Existing doctests already covered str and float.
  • Updated the parametrized test values to a list to satisfy Ruff PT007.

Testing

Ran locally:

  • python3 -m doctest -v sorts/bubble_sort.py — 42 passed, 0 failed
  • Direct runtime checks: strings and floats sort correctly; [1, "a"] raises TypeError for both functions
  • python3 -c AST parse of both changed files succeeded
  • GitHub Actions Ruff check now passes on the latest head commit

Also added test_bubble_sort_rejects_non_comparable_items in tests/test_sorts.py for both functions.

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

Bound bubble_sort_iterative and bubble_sort_recursive to a Comparable protocol instead of Any, add TypeError doctests, and cover the mixed-type failure in tests/test_sorts.py.

Refs TheAlgorithms#15234
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Sep 9, 2026
@algorithms-keeper

Copy link
Copy Markdown

Closing this pull request as invalid

@HarshRajSinghania, 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 Sep 9, 2026
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 9, 2026
@cclauss cclauss reopened this Sep 9, 2026
@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files tests are failing Do not merge until tests pass labels Sep 9, 2026
Comment thread tests/test_sorts.py Outdated

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

Nice work @HarshRajSinghania — this faithfully follows the insertion_sort.py reference pattern:

  • Comparable protocol + PEP 695 [T: Comparable] on both the iterative and recursive variants ✅
  • +IGNORE_EXCEPTION_DETAIL doctest showing the TypeError on mixed [1, "a"]
  • parametrized test_bubble_sort_rejects_non_comparable_items covering both entry points ✅

Both functions share the same protocol, so mixed-type inputs fail fast exactly like the reference. CI is green and it's mergeable. LGTM from me — deferring to @cclauss for the merge. Thanks for jumping on this one!

@cclauss

cclauss commented Sep 9, 2026

Copy link
Copy Markdown
Member

CI is green

Not.

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Sep 9, 2026
Updated tests to include binary insertion sort and renamed test function.
@algorithms-keeper algorithms-keeper Bot removed tests are failing Do not merge until tests pass labels Sep 9, 2026
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 9, 2026
@cclauss
cclauss merged commit 5ce6733 into TheAlgorithms:master Sep 9, 2026
6 checks passed
@HarshRajSinghania

Copy link
Copy Markdown
Contributor Author

@priya-sundaram-dev Thanks! I’ve taken care of the follow-up on the PR in this pass:

  • Updated the pytest parametrization to use a list, addressing Ruff PT007.
  • Refreshed the PR description and contributor checklist.
  • Changed the issue reference to Fixes #15234.
  • Verified the latest CI is green: Ruff, ty, build, and build_docs all pass.

Thanks again for the review and the kind words! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement This PR modified some existing files invalid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sorts: make algorithms sort any comparable items, not just ints

3 participants