Skip to content

sorts: type selection sort for comparable items - #15245

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

sorts: type selection sort for comparable items#15245
cclauss merged 3 commits into
TheAlgorithms:masterfrom
HarshRajSinghania:sorts/selection-sort-comparable

Conversation

@HarshRajSinghania

Copy link
Copy Markdown
Contributor

Part of #15234

Make selection_sort correctly typed for any mutually comparable items instead of only list[int].

Changes:

  • Use a Comparable protocol with the existing generic type-parameter pattern.
  • Accept and return a MutableSequence[T].
  • Add doctests covering strings and floats in addition to integers.
  • Keep the selection-sort algorithm itself unchanged.

Testing:

  • python -m doctest -v sorts/selection_sort.py — 7 passed
  • python -m pytest tests/test_sorts.py -q — 185 passed

@cclauss

cclauss commented Sep 9, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev, your review, please.

Comment thread sorts/selection_sort.py
[0]

>>> selection_sort([2, -3, 0, 5, -1])
[-3, -1, 0, 2, 5]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PLEASE do not remove tests!!!

Comment thread sorts/selection_sort.py
>>> selection_sort(["d", "a", "c", "b"])
['a', 'b', 'c', 'd']

>>> selection_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PLEASE do not remove tests!

@HarshRajSinghania

HarshRajSinghania commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Sorry about that... I accidentally removed the existing doctest while updating the type annotations. I've restored the original selection-sort doctests, including the selection_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2]) test, and kept the new comparable-item coverage. Thanks for catching that.

@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! ✅ This matches the pattern we settled on in #15234 — a minimal Comparable protocol (just __lt__, which is all selection sort needs), the generic [T: Comparable] type parameter, and MutableSequence[T] in and out. The algorithm body is untouched, and the added string/float doctests are a nice touch. Ran the doctests locally — all 13 pass. Thanks for restoring the original doctests after the earlier slip; looks clean now. 🙌

@cclauss
cclauss merged commit 0372abc into TheAlgorithms:master Sep 9, 2026
6 checks passed
@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

Perfect—restored doctest plus the new comparable-item coverage is exactly right, and the quick turnaround was great. Thanks @HarshRajSinghania, and welcome aboard! 🎉

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants