Skip to content

Select median-of-medians pivots from groups of five - #15847

Open
6iu8a wants to merge 1 commit into
TheAlgorithms:masterfrom
6iu8a:fix/median-of-five-groups
Open

6iu8a wants to merge 1 commit into
TheAlgorithms:masterfrom
6iu8a:fix/median-of-five-groups

Conversation

@6iu8a

@6iu8a 6iu8a commented Dec 3, 2026

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?

median_of_medians passes the entire remaining suffix to median_of_five whenever at least four items remain. For input [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], the two groups have medians 8 and 3. The function uses the upper median, so the pivot should be 8. It currently returns 6.

Take one slice of up to five items per group, including the final partial group. This also avoids repeatedly sorting overlapping suffixes.

All 12 existing doctests pass. I verified that every group passed to median_of_five had at most five elements for 303 inputs, and that quick_select agreed with sorting for all 15,453 ranks in those inputs. Ruff lint and format checks passed for the file. AI-assisted 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. To ease review, please open separate PRs for separate algorithms.
  • 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.
  • 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. (Existing algorithm.)
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword. (No matching open issue found.)

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

1 participant