Skip to content

fix(shell-sort): stop insertion at an ordered gap pair - #2244

Open
Shubham-Padkonde wants to merge 1 commit into
trekhleb:masterfrom
Shubham-Padkonde:fix/shell-sort-ordered-gap
Open

Shubham-Padkonde wants to merge 1 commit into
trekhleb:masterfrom
Shubham-Padkonde:fix/shell-sort-ordered-gap

Conversation

@Shubham-Padkonde

Copy link
Copy Markdown

Fixes #2234.

Shell sort currently walks the entire gap subsequence even after a comparison needs no swap. Stop that insertion pass at the first ordered pair, since the earlier portion of the gap subsequence is already sorted.

For 4,000 sorted values, this reduces comparisons from 12,831,965 to 40,006. Add a regression bounding visits by n * ceil(log2(n)), and update the existing exact visit counts to reflect the reduced work. Sorting results and input preservation remain covered by the existing tests.

Validation: the new regression fails on the original implementation. The complete test suite with coverage and ESLint pass.

AI assistance was used to investigate, implement, and validate this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ShellSort: best case is O(n虏), README says O(n log n), inner loop never breaks

1 participant