Quick sort in 4 minutes
Channel: Michael Sambol
Watch on YouTube →Algorithm • hard
Quick sort partitions around a pivot so smaller values move left and larger values move right.
It is often one of the fastest general-purpose sorts in practice due to cache-friendly partitioning.
Its worst case is O(n^2), so pivot strategy and randomization matter.
Typical Complexity Baseline
| Metric | Value |
|---|---|
| Average | O(n log n) |
| Worst | O(n^2) |
| Space | O(log n) recursion |
See It
A small animated SVG that illustrates the core idea the moving parts you need to keep in your head when you read the rest of the guide.
After partitioning, every left-side value is ≤ pivot and every right-side value is > pivot.
Watch First
A quick visual walkthrough before the detailed sections pick the format that helps you learn fastest.
Channel: Michael Sambol
Watch on YouTube →Mechanics
The building blocks and terminology you need before everything else clicks. Skim or deep-read.
What it is Chosen value used to split array into two regions.
Why it matters Pivot selection is a required building block for understanding how Quick Sort stays correct and performant on large inputs.
What it is Reorder elements around pivot boundary.
Why it matters Partition procedure is a required building block for understanding how Quick Sort stays correct and performant on large inputs.
What it is Sort left and right partitions recursively.
Why it matters Recursive subproblems is a required building block for understanding how Quick Sort stays correct and performant on large inputs.
What it is Tail recursion/randomized pivot to limit worst-case behavior.
Why it matters Recursion depth control is a required building block for understanding how Quick Sort stays correct and performant on large inputs.
What it is Partition done directly on original array.
Why it matters In-place swaps is a required building block for understanding how Quick Sort stays correct and performant on large inputs.
What it is Reference element used for partitioning step.
Why it matters Reference element used for partitioning step. In Quick Sort, this definition helps you reason about correctness and complexity when inputs scale.
What it is Final pivot location after partitioning.
Why it matters Final pivot location after partitioning. In Quick Sort, this definition helps you reason about correctness and complexity when inputs scale.
What it is Random pivot choice to reduce adversarial patterns.
Why it matters Random pivot choice to reduce adversarial patterns. In Quick Sort, this definition helps you reason about correctness and complexity when inputs scale.
What it is Optimization style to reduce stack growth.
Why it matters Optimization style to reduce stack growth. In Quick Sort, this definition helps you reason about correctness and complexity when inputs scale.
What it is Partition-based algorithm to find kth statistic without full sort.
Why it matters Partition-based algorithm to find kth statistic without full sort. In Quick Sort, this definition helps you reason about correctness and complexity when inputs scale.
Walkthrough
Every frame of the concept diagram, laid out top-to-bottom so you can scroll through the algorithm at your own pace.
Pick pivot = arr[last] = 4. i = boundary (left of which everything ≤ pivot). j = scan.
j=0 · arr[0]=7 > 4 → skip. i stays.
j=1 · arr[1]=7 ≤ 4 → swap with i=0. i++.
j=2 · arr[2]=9 > 4 → skip. i stays.
j=3 · arr[3]=7 ≤ 4 → swap with i=1. i++.
j=4 · arr[4]=5 > 4 → skip. i stays.
j=5 · arr[5]=8 > 4 → skip. i stays.
j=6 · arr[6]=9 ≤ 4 → swap with i=2. i++.
j=7 · arr[7]=6 > 4 → skip. i stays.
Final swap: arr[3] ↔ pivot. Pivot 4 sits at index 3. Left of it: ≤4. Right: >4. Recurse on each side (not shown).
Tradeoffs
When this technique wins, when it loses, and what to reach for instead.
Choose this when Choose quick sort for in-place average-case speed.
Tradeoff Merge sort has better worst-case guarantee and stability.
Choose this when Choose quick sort for practical cache-local performance.
Tradeoff Heap sort avoids O(n^2) worst case but can be slower in practice.
Choose this when Choose quick sort for any real production sorting workload.
Tradeoff Bubble is only useful for pedagogy or tiny lists.
In Production
Where this shows up at real companies and what the on-call engineer learned the hard way.
Many standard library sort implementations use quicksort-inspired partitioning hybrids (often introsort variants).
Takeaway Quick sort ideas are still central in production-grade sort engines.
Ranking pipelines repeatedly order candidate sets where partition-style selection helps prune work.
Takeaway Partitioning concepts help both full sorting and top-k retrieval.
Latency-sensitive auction logic benefits from fast in-memory ordering of medium-size candidate arrays.
Takeaway Average-case speed and memory locality are decisive in low-latency loops.
Code
A clean reference implementation in TypeScript, Python, and Go. Switch languages with the toggle.
Partition around pivot, then recursively sort left and right partitions.
Complexity: Average O(n log n), Worst O(n^2), Space O(log n) recursion average
Lomuto-partition quick sort
function quickSort(values: number[], leftIndex = 0, rightIndex = values.length - 1): number[] {
if (leftIndex >= rightIndex) return values
const pivotValue = values[rightIndex]
let storeIndex = leftIndex
for (let scanIndex = leftIndex; scanIndex < rightIndex; scanIndex += 1) {
if (values[scanIndex] <= pivotValue) {
;[values[storeIndex], values[scanIndex]] = [values[scanIndex], values[storeIndex]]
storeIndex += 1
}
}
;[values[storeIndex], values[rightIndex]] = [values[rightIndex], values[storeIndex]]
quickSort(values, leftIndex, storeIndex - 1)
quickSort(values, storeIndex + 1, rightIndex)
return values
}
Watch Out
The bugs and edge cases that bite engineers most often. Skim before you ship.
O(n^2).Pro Tips
Small habits that pay off every time you reach for this pattern.
Pattern Recognition
Concrete signals that tell you this is the right pattern both at work and on LeetCode.
Practice
A curated easy-to-hard problem ladder. Each one reinforces a specific aspect of the pattern.
| # | Problem | Difficulty | Typical Complexity |
|---|---|---|---|
| 1 | Sort an Array | Medium | O(n log n) average |
| 2 | Kth Largest Element in an Array | Medium | O(n) avg quickselect |
| 3 | Top K Frequent Elements | Medium | O(n log k) |
| 4 | Wiggle Sort II | Medium | O(n log n) |
| 5 | Largest Number | Medium | O(n log n) |
| 6 | Sort Colors | Medium | O(n) |
| 7 | K Closest Points to Origin | Medium | O(n log k) |
| 8 | Median of Two Sorted Arrays | Hard | O(log(min(m,n))) |
| 9 | Count of Smaller Numbers After Self | Hard | O(n log n) |
| 10 | Reverse Pairs | Hard | O(n log n) |