Skip to content

Fix flash_sort permutation phase raising IndexError on repeated values - #15380

Open
Darkslayer3324j wants to merge 1 commit into
TheAlgorithms:masterfrom
Darkslayer3324j:fix/flash-sort-permutation
Open

Darkslayer3324j wants to merge 1 commit into
TheAlgorithms:masterfrom
Darkslayer3324j:fix/flash-sort-permutation

Conversation

@Darkslayer3324j

Copy link
Copy Markdown
Contributor

Describe your change

  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.

sorts.flash_sort.flash_sort raises IndexError for a number of ordinary inputs, for example:

>>> flash_sort([6, 6, 4, 4, 6])
IndexError: list index out of range

Found by comparing every function in sorts/ against sorted() 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 walk k and j past 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 - 1 moves 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 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. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • 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.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER". (No open issue for this.)

I left "all my own work" unticked on purpose: this change was written with AI assistance (Claude Code, as AGENTS.md invites) and follows the published flashsort permutation scheme rather than being an original design. I reproduced the failure and ran the doctests, ruff check and ruff format --check on the changed file.

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

zhudingsuifeng commented Sep 19, 2026 via email

Copy link
Copy Markdown

@BCGL

BCGL commented Sep 19, 2026 via email

Copy link
Copy Markdown

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.

3 participants