Skip to content

sorts: make recursive_insertion_sort generic over Comparable items - #15340

Open
Rokesh2008 wants to merge 1 commit into
TheAlgorithms:masterfrom
Rokesh2008:feat/recursive-insertion-sort-comparable
Open

Rokesh2008 wants to merge 1 commit into
TheAlgorithms:masterfrom
Rokesh2008:feat/recursive-insertion-sort-comparable

Conversation

@Rokesh2008

Copy link
Copy Markdown

Part of #15234

This is an in-place sort, so it follows the MutableSequence[T] bucket of the Comparable/TypeVar convention discussed on #15234, rather than the Iterable[T] -> list[T] bucket used by copy-and-return sorts:

  • rec_insertion_sort/insert_next now take MutableSequence[T] bound to Comparable
  • rec_insertion_sort now returns the sorted collection (previously -> None) and n defaults to len(collection), so it can be called with a single argument — needed so it fits the shared SORTS battery in tests/test_sorts.py, which calls every sort as sort(list(case))
  • added a string doctest alongside the existing int examples
  • registered rec_insertion_sort in the shared SORTS battery (checked against sorted(), including the Person/Dog comparable objects) and in test_sort_rejects_non_comparable_items

Tests:

  • python -m doctest -v sorts/recursive_insertion_sort.py — 18/18 passed
  • Verified rec_insertion_sort against every case in tests/test_sorts.py's CASES tuple with a single-argument call, matching sorted()
  • Confirmed rec_insertion_sort([1, "a"]) raises TypeError

Linking with Part of #15234 rather than a closing keyword, per the issue's contribution guidance.

Part of TheAlgorithms#15234

- switch rec_insertion_sort/insert_next to the Comparable/TypeVar-bound
  MutableSequence[T] pattern (in-place sorts bucket) per the convention
  discussed on TheAlgorithms#15234
- make rec_insertion_sort return the sorted collection and give n a
  default of len(collection), so it can be called with a single
  argument like the other sorts in the shared test battery
- add a string doctest
- register rec_insertion_sort in tests/test_sorts.py's shared SORTS
  battery and the non-comparable-items rejection test
@cclauss

cclauss commented Sep 15, 2026

Copy link
Copy Markdown
Member

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

@cclauss

cclauss commented Sep 15, 2026

Copy link
Copy Markdown
Member

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

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants