Skip to content

Use last element as QuickSort pivot#2185

Open
natecl wants to merge 1 commit into
trekhleb:masterfrom
natecl:codex/quick-sort-last-pivot-265
Open

Use last element as QuickSort pivot#2185
natecl wants to merge 1 commit into
trekhleb:masterfrom
natecl:codex/quick-sort-last-pivot-265

Conversation

@natecl

@natecl natecl commented Jun 16, 2026

Copy link
Copy Markdown

Summary

  • Use the last element as the QuickSort pivot via array.pop() instead of using array.shift() from the front of the array.
  • Preserve stable sorting by appending the popped pivot after equal elements that appeared before it.
  • Update the expected visit count for the not-sorted fixture from 62 to 85.

Closes #265.

Verification

  • npx jest src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js --runInBand
  • npx eslint src/algorithms/sorting/quick-sort/QuickSort.js src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js

Note

Related: #171 proposes a broader median-of-three QuickSort pivot change and also touches Kruskal. This PR is intentionally narrower and only addresses the shift() pivot cost described in #265.

@natecl natecl marked this pull request as ready for review June 16, 2026 20:01
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.

QuickSort : Instead of taking pivot as 1st element, we should take the pivot as last element, due to time complexity

1 participant