Skip to content

fix(sorts): support less-than-only items in iterative merge sort - #15690

Open
parijat1222q wants to merge 1 commit into
TheAlgorithms:masterfrom
parijat1222q:fix/iterative-merge-lt-protocol
Open

parijat1222q wants to merge 1 commit into
TheAlgorithms:masterfrom
parijat1222q:fix/iterative-merge-lt-protocol

Conversation

@parijat1222q

Copy link
Copy Markdown

Describe your change

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

Comparable in sorts/iterative_merge_sort.py requires only __lt__, but merge uses <=. Values that implement the declared protocol can therefore be sorted by Python's sorted and the sibling merge_sort, yet fail here with TypeError.

Select from the right when right[0] < left[0]; otherwise select from the left. This uses only the declared operation and keeps the left-first ordering for equal keys. It does not change the existing pop(0) implementation or other algorithms.

Related contract: #15234 and #15413. This is a runtime fix, not another type-annotation change. No closing keyword is used for the umbrella issue.

Reproduction

On master at 574d100b7181956e9c8384552fd078165e4cc326, with Python 3.15t:

from sorts.iterative_merge_sort import iter_merge_sort

class Item:
    def __init__(self, key):
        self.key = key

    def __lt__(self, other):
        return self.key < other.key

items = [Item(2), Item(1)]
print([item.key for item in iter_merge_sort(items)])

Before: TypeError: '<=' not supported between instances of 'Item' and 'Item'.
After: [1, 2].

Validation

The current contribution guide asks not to change code and doctests in the same PR, so this changes one algorithm source file only. Existing doctests are unchanged.

  • External local regression failed on the base and passed with this fix. It checks all 1,093 key sequences of lengths 0 through 6 over keys {0, 1, 2}, using objects that define only __lt__; compares object identity/order with sorted (including stable ties), checks input preservation, exercises merge directly, and retains TypeError for [1, "a"].
  • uv run --python 3.15t --no-project --with pytest python -m pytest -q -o addopts= tests/test_sorts.py sorts/iterative_merge_sort.py --doctest-modules: 456 passed.
  • ruff check sorts/iterative_merge_sort.py and ruff format --check sorts/iterative_merge_sort.py: passed.
  • Tested on Linux. The full repository suite and native Windows have not been run for this target 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.
  • All new Python files are placed inside an existing directory. (No new files.)
  • 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. (Unchanged.)
  • All functions have doctests that pass the automated testing. (Existing selected doctests pass locally; CI pending.)
  • All new algorithms include at least one reference URL. (No new algorithm.)
  • If this pull request resolves open issues, the description includes closing keywords. (No open issue is claimed resolved.)

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Nov 4, 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 reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants