Skip to content

feat(sorts): make odd-even transposition generic over comparable items - #15405

Merged
cclauss merged 2 commits into
TheAlgorithms:masterfrom
5h4d0wn1k:sorts/make-odd-even-transposition-generic
Sep 23, 2026
Merged

cclauss merged 2 commits into
TheAlgorithms:masterfrom
5h4d0wn1k:sorts/make-odd-even-transposition-generic

Conversation

@5h4d0wn1k

Copy link
Copy Markdown
Contributor

Part of #15234

Replaces bare list[Any] with a Comparable-bounded PEP 695 type parameter so odd_even_transposition sorts any mutually comparable items (ints, floats, strings, dataclasses, namedtuples), not just ints.

  • Switches to the Comparable Protocol + generic pattern used by the merged reversort/`insertion_sort` PRs
  • Preserves in-place semantics (asserted with a result is values doctest)
  • Adds success doctests for int/float/str and a TypeError rejection doctest for mixed non-comparable input
  • Registers the sort in the shared battery (`SORTS`) and in `test_sort_rejects_non_comparable_items`

Verification

  • ruff check + ruff format --check clean (ruff 0.16.7, repo config)
  • python -m doctest on the module: 18 doctests pass
  • tests/test_sorts.py: 296 battery tests pass

Part of TheAlgorithms#15234

- Replace list[Any] with a Comparable-bounded TypeVar so
  odd_even_transposition sorts any mutually comparable items,
  not just ints
- Add str/float/int success doctests plus a TypeError rejection
  doctest for non-comparable mixed input
- Assert in-place behavior via `result is values` doctest
- Register the sort in the shared battery and in
  test_sort_rejects_non_comparable_items

Co-authored-by: ayushman-it <253447966+ayushman-it@users.noreply.github.com>
@cclauss

cclauss commented Sep 23, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev, please review. Are the type hints as expected?

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

The type hints look correct to me 👍

  • The PEP 695 syntax `odd_even_transposition[T: Comparable](collection: list[T]) -> list[T]` is the right call here, and returning `list[T]` (same element type) matches the in-place behavior the doctests assert (`result is values`).
  • Scoping the `Comparable` protocol to just `lt` is spot-on, since `<` is the only ordering operation the algorithm actually uses — no reason to require the full rich-comparison set.

I ran the doctests and the property-style checks (random int/str samples vs `sorted()`, plus the mixed-type `TypeError` case) and they all pass on 3.12/3.13.

One tiny, optional nit: the module docstring still says "python3 -m doctest -v odd_even_transposition_single_threaded.py" — worth double-checking that filename matches after any renames, but it looks right as-is. Nice cleanup overall.

@cclauss
cclauss merged commit 60cea20 into TheAlgorithms:master Sep 23, 2026
6 checks passed
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