Fix flash_sort permutation phase raising IndexError on repeated values - #15380
Open
Darkslayer3324j wants to merge 1 commit into
Open
Darkslayer3324j wants to merge 1 commit into
Darkslayer3324j wants to merge 1 commit into
Conversation
flash_sort([6, 6, 4, 4, 6]) raised IndexError (about 2% of random small inputs). Replace the permutation phase with the standard cycle-leader version and add doctests for two previously failing inputs. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
您好,邮件已经收到,谢谢
|
|
您好,邮件已收到!
|
13 tasks
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
sorts.flash_sort.flash_sortraisesIndexErrorfor a number of ordinary inputs, for example:Found by comparing every function in
sorts/againstsorted()on random lists. About 2% of random lists of 2-6 small integers fail (212 of 12,000 in my run), so it is not limited to an odd corner: the permutation phase can walkkandjpast the class table when a class holds repeated values.The fix replaces the permutation phase with the standard cycle-leader loop from the flashsort description (https://en.wikipedia.org/wiki/Flashsort): class sizes become exclusive end positions, and each element is swapped to the end of its class until
n - 1moves have been made. The classification, class count and final insertion sort are unchanged.Doctests for two previously failing inputs are added to the docstring (the only test change, and it lives with the code it covers). Beyond the doctests I compared the new version against
sorted()on 64,000 random lists (lengths 0-39; negatives, floats, heavy duplicates): 0 mismatches or exceptions, versus failures before.Checklist
I left "all my own work" unticked on purpose: this change was written with AI assistance (Claude Code, as
AGENTS.mdinvites) and follows the published flashsort permutation scheme rather than being an original design. I reproduced the failure and ran the doctests,ruff checkandruff format --checkon the changed file.