DSA Notes
Master quick sort completely — partition schemes (Lomuto and Hoare), randomized quick sort, 3-way partition for duplicates, average-case proof, worst-case prevention, why quick sort beats merge sort in practice despite equal asymptotic complexity, and tail-call optimization.
The Core Idea
Quick sort partitions the array around a pivot element so that:
- All elements smaller than pivot go to its left
- All elements larger than pivot go to its right
- The pivot is now in its final sorted position
Then recursively sort the left and right partitions.
Randomized Quick Sort
The problem: Worst case O(n²) occurs when pivot always picks the smallest or largest element — e.g., sorted array with last-element pivot.
The fix: Choose a random pivot before partitioning.
Why randomization works:
- Any specific "bad" input is only bad for one particular pivot sequence
- With random pivots, the probability that every pivot is adversarial is (1/n)ⁿ — essentially zero
- Expected number of comparisons: 2n ln(n) ≈ 1.39 n log₂n
3-Way Partition — Handles Duplicates Efficiently
When the array has many duplicate elements, standard Quick Sort degrades. The Dutch National Flag 3-way partition handles this:
Complexity Analysis
Best Case — Ω(n log n): Perfectly Balanced Pivots
Average Case — Θ(n log n): Random Input
The expected number of comparisons when each permutation is equally likely:
Quick Sort does about 39% more comparisons than Merge Sort on average, but is faster in practice due to better cache behavior.
Worst Case — O(n²): Always Bad Pivot
| Partition [1,2,3,4,5] | pivot=5, left=[1,2,3,4], right=[] |
| Partition [1,2,3,4] | pivot=4, left=[1,2,3], right=[] |
| T(n) = T(n-1) + O(n) | O(n²) |
Prevention:
- Random pivot selection
- Median-of-three pivot (choose median of first, middle, last)
- 3-way partition for duplicate-heavy inputs
Why Quick Sort Beats Merge Sort in Practice
Despite equal Θ(n log n) average complexity, Quick Sort is faster in practice:
1. Cache Friendliness
| Merge Sort: creates new arrays | random memory access → cache misses |
| Quick Sort: in-place swaps | sequential memory access → cache hits |
| Cache miss penalty | ~100x slower than cache hit |
| For modern processors | Quick Sort is 2-4x faster in practice |
2. Memory Allocation
3. Better Constant Factors
Quick Sort's inner loop
while arr[i] < pivot: i++ ← one comparison, no extra work
Merge Sort's inner loop
if arr[i] <= arr[j]: ← comparison
result.append(arr[i]) ← copy to new array (expensive)
i++
4. Recursion Depth
| Merge Sort | always log n levels |
| Quick Sort (average) | log n levels, but much smaller constant |
| Quick Sort (worst): n levels | tail-call optimization helps |
Tail-Call Optimization for Quick Sort
def quicksort_tco(arr, low, high):
"""
Tail-call optimized: always recurse into the smaller partition first.
Guarantees O(log n) stack depth regardless of pivot quality.
"""
while low < high:
pi = lomuto_partition(arr, low, high)
if pi - low < high - pi:
# Left partition is smaller → recurse left, loop right
quicksort_tco(arr, low, pi - 1)
low = pi + 1
else:
# Right partition is smaller → recurse right, loop left
quicksort_tco(arr, pi + 1, high)
high = pi - 1By always recursing into the smaller partition and using a loop for the larger, the maximum recursion depth is O(log n) even in the worst case.
Summary
| Property | Quicksort |
|---|---|
| Best case | O(n log n) |
| Average case | O(n log n) — ~1.39n log n comparisons |
| Worst case | O(n²) — prevented with random pivot |
| Space | O(log n) avg, O(n) worst (with TCO: O(log n) always) |
| Stable | ❌ No |
| Adaptive | ❌ No (without 3-way partition) |
| Cache friendly | ✅ Yes — in-place access |
Choose Quick Sort when:
- Average performance matters most
- Memory is limited (O(log n) space)
- Data is random or uniformly distributed
- Used with randomization (eliminates adversarial inputs)
Choose Merge Sort instead when:
- Stability is required
- Guaranteed O(n log n) worst case needed
- Sorting linked lists
*Next Lesson: Heap Sort*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Quick Sort — The Fastest Practical Sorting Algorithm with Complete Analysis.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, sorting, quick, sort
Related Data Structures & Algorithms Topics