fix: handle empty input in tim_sort - #14805
Merged
cclauss merged 2 commits intoSep 6, 2026
Merged
Conversation
jordandunmire97-ai
approved these changes
Jun 28, 2026
priya-sundaram-dev
approved these changes
Sep 6, 2026
priya-sundaram-dev
left a comment
Contributor
There was a problem hiding this comment.
Good bug fix. Without the guard, tim_sort([]) raises IndexError on new_run = [lst[0]]; the early return [] handles the empty case and the added doctest locks in the behaviour. Correct and minimal — thanks! ✅
cclauss
enabled auto-merge (squash)
September 6, 2026 06:09
cclauss
approved these changes
Sep 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
Fixed
tim_sort([])so it returns an empty list instead of raisingIndexErrorwhen the implementation tries to readlst[0].Why
Sorting an empty sequence should be a valid no-op. Other sort implementations in this repository handle empty input, and Python's
sorted([])returns[].How tested
/opt/homebrew/opt/python@3.14/bin/python3.14 -m doctest -v sorts/tim_sort.pyfailed withIndexError: list index out of rangebefore the fix/opt/homebrew/opt/python@3.14/bin/python3.14 -m doctest -v sorts/tim_sort.py/tmp/thealgorithms-py314-pytest/bin/python -m pytest --doctest-modules sorts/tim_sort.py -qtim_sort(case)againstsorted(case)for 100 generated listsgit diff --checkChecklist: