Sorting Algorithms
Quick Sort & Quick Select
Partition-based sorting and selection — O(N log N) average sort, O(N) average selection.
Last Updated: August 2, 2026
•
25 min read
1. Introduction
What is Quick Sort?
Quick Sort is an highly efficient, in-place sorting algorithm that uses a Divide and Conquer partitioning strategy. It chooses a "pivot" element and rearranges the array so that all elements smaller than the pivot go to its left, and all larger elements go to its right.Why is it Important?
O(N²), Quick Sort is typically faster than Merge Sort in practice because of low constant factors and excellent CPU cache locality.K-th smallest/largest element in an unsorted array in average O(N) time (instead of O(N log N) sorting).Where is it Used?
std::sort uses Introsort, which starts with Quick Sort and switches to Heap Sort if the recursion depth exceeds a threshold.Arrays.sort() uses a Dual-Pivot Quicksort implementation.2. Mental Model: Team Captain (Pivot)
Think of partitioning as picking a team captain (the pivot).
We then repeat this process recursively for the left and right groups.
3. Core Concepts & Implementations
Lomuto vs. Hoare Partitioning
Quick Select: Finding K-th Element in O(N) Average Time
Unlike Quick Sort which recurses into both partitioned halves, Quick Select checks if the pivot's final index matches our target indexK. If it does, we return it. Otherwise, we recurse only into the half containing K.4. Visual Trace: Lomuto Partitioning
Let's partition [4, 2, 7, 3, 5] around pivot 5 (last element):
5. Real-World Applications
O(N) time instead of O(N log N).O(N²) degradation).6. Interview Perspective
How Interviewers Ask This Topic
O(N) average time solution.Common Mistakes
Warning: 1. Degenerate O(N²) Performance on Sorted Inputs: Picking the first or last element as the pivot in an already-sorted array results in highly unbalanced splits (
1 element vs N - 1 elements). This leads to O(N²) runtime. Always select a random pivot or use median-of-three selection in interviews.> 2. Stack Overflow Risks: Standard Quicksort recursively processes both sides. To limit recursive call stack depth to O(log N) in the worst case, always recurse into the smaller partition first, and use tail-call elimination for the larger partition.
7. Summary
O(N²) worst-case time.O(N) average time.8. Quiz
Question 1: What is the recurrence relation for the worst-case time complexity of Quick Sort, and what causes it?
Answer:T(N) = T(N - 1) + O(N), resolving to O(N²). This occurs when the pivot chosen is always the absolute minimum or maximum element of the partition (e.g., sorting an already-sorted array with first/last element chosen as the pivot).
Question 2: How does randomized pivot selection guarantee O(N log N) time?
Answer: Selecting a pivot uniformly at random prevents adversaries from feeding input arrays that trigger the worst-case partitioning. The mathematical expectation of partition splits is highly balanced (O(N log N) average), with the worst case O(N²) having a probability approaching 0.
Question 3: Why is Quick Select average-case time complexity O(N), whereas Quick Sort is O(N log N)?
Answer: Quick Sort solves both partitions:T(N) = 2T(N/2) + O(N) \implies O(N log N).
Quick Select only solves one partition: T(N) = T(N/2) + O(N). Using the geometric series summation: N + N/2 + N/4 + ... ≤ 2N, resulting in O(N) time.