Quick sort in 4 minutes
Channel: Michael Sambol
Full video link:
https://www.youtube.com/watch?v=Hoixgm4-P4MAlgorithm • 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 |
Prefer video learning? This explainer gives a quick visual walkthrough of the core idea before you dive into the detailed sections below.
Quick sort in 4 minutes
Channel: Michael Sambol
Full video link:
https://www.youtube.com/watch?v=Hoixgm4-P4MStep through Quick Sort to see how the list changes over time.
Start with unsorted values.
Learn the core building blocks and terminology in one place before comparisons, so the mechanics are clear and duplicates are removed.
Pivot selection
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.
Partition procedure
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.
Recursive subproblems
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.
Recursion depth control
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.
In-place swaps
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.
Pivot
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.
Partition index
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.
Randomized quicksort
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.
Tail recursion
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.
Quickselect
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.
This walkthrough connects the core concepts of Quick Sort into one end-to-end execution flow.
Step 1
Chosen value used to split array into two regions.
Before
Array
After
Partitioned
Transition
Why this step matters: Pivot selection is a required building block for understanding how Quick Sort stays correct and performant on large inputs.
Step 2
Reorder elements around pivot boundary.
Before
Left partition
After
Transition
Why this step matters: Partition procedure is a required building block for understanding how Quick Sort stays correct and performant on large inputs.
Step 3
Sort left and right partitions recursively.
Before
Subarray
After
Subarray becomes
Transition
Why this step matters: Recursive subproblems is a required building block for understanding how Quick Sort stays correct and performant on large inputs.
Step 4
Tail recursion/randomized pivot to limit worst-case behavior.
Before
After
Sorted array
Transition
Why this step matters: Recursion depth control is a required building block for understanding how Quick Sort stays correct and performant on large inputs.
vs Merge Sort
When to choose this: Choose quick sort for in-place average-case speed.
Tradeoff: Merge sort has better worst-case guarantee and stability.
vs Heap Sort
When to choose this: Choose quick sort for practical cache-local performance.
Tradeoff: Heap sort avoids O(n^2) worst case but can be slower in practice.
vs Bubble Sort
When to choose this: Choose quick sort for any real production sorting workload.
Tradeoff: Bubble is only useful for pedagogy or tiny lists.
Language runtimes
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.
Search ranking systems
Ranking pipelines repeatedly order candidate sets where partition-style selection helps prune work.
Takeaway: Partitioning concepts help both full sorting and top-k retrieval.
Ad-tech bidding systems
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.
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
}
O(n^2).Use these signals to decide if this data structure/algorithm is the right fit before implementation.
| # | 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) |