Quicksort partitioning and pivot trade-offs
Quick answer
Quicksort partitions values around a pivot and recursively sorts the smaller and larger groups. The sizes of those groups determine the work and recursion depth.
Working examples
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const a=[4,1,4,8,2],pivot=4;const groups=[a.filter(x=>x<pivot),a.filter(x=>x===pivot),a.filter(x=>x>pivot)];console.log(JSON.stringify(groups));Expected console output:
[[1,2],[4,4],[8]]All duplicates of the pivot remain in the equal group.
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
function quick(a){ if(a.length<2)return a.slice(); const p=a[0],l=[],e=[],r=[]; for(const x of a){if(x<p)l.push(x);else if(x>p)r.push(x);else e.push(x);} return [...quick(l),...e,...quick(r)];}console.log(JSON.stringify(quick([3,1,3,2])),JSON.stringify(quick([5,5,5])));Expected console output:
[1,2,3,3] [5,5,5]The equal partition preserves multiplicity and needs no recursive sort.
How it works and cost model
The educational three-way version selects a pivot and builds less, equal and greater arrays, then recursively sorts less and greater. Equal values are emitted once as a group, preventing repeated processing of an all-equal input. Balanced partitions give O(n log n) work; highly skewed partitions can give O(n²). Randomized pivot selection gives expected O(n log n) comparisons under common models, not a worst-case guarantee. This implementation allocates arrays at each call and is not an in-place quicksort; skew can retain quadratic total live elements along a deep call chain. Large inputs may exceed call-stack limits. Do not infer stability of all quicksort variants.
Common mistakes
Do not lose duplicate values by emitting just one pivot. Guarantee that the recursive groups are strictly smaller; never claim deterministic first-pivot quicksort is always fast.
Practice
Partition [4,1,4,8,2] around 4; report group contents, then compare first-pivot behavior on ascending and all-equal arrays.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Quicksort partitioning and pivot trade-offs — Knowledge Check
Reason about the algorithm or data structure, its invariants, trade-offs and deterministic outputs.
What creates the quadratic worst case for ordinary quicksort?
Why retain an equal partition in three-way quicksort?
Which statement applies to this array-building quicksort?
What is the exact console output in the Three-way quicksort example 1?
const a=[4,1,4,8,2],pivot=4;const groups=[a.filter(x=>x<pivot),a.filter(x=>x===pivot),a.filter(x=>x>pivot)];console.log(JSON.stringify(groups));What is the exact console output in the Three-way quicksort example 2?
function quick(a){ if(a.length<2)return a.slice(); const p=a[0],l=[],e=[],r=[]; for(const x of a){if(x<p)l.push(x);else if(x>p)r.push(x);else e.push(x);} return [...quick(l),...e,...quick(r)];}console.log(JSON.stringify(quick([3,1,3,2])),JSON.stringify(quick([5,5,5])));