Abstract
Quick Sort picks a random pivot, partitions the list around it, and recursively sorts both sides — the randomized counterpart to Computer Science Introduction/Algorithms/Divide and Conquer/Merge Sort’s deterministic split.
- Category: Divide and Conquer / Sorting (Randomized)
- Input: A list
- Output: The list, sorted
- Paradigm: Randomized Divide and Conquer
- Typical use cases: general-purpose in-place sorting; often faster in practice than Merge Sort due to good cache locality and a smaller constant factor, despite a worse worst-case bound
Core Logic (High-Level)
- Pick a random index and treat as the pivot.
- Partition the list into (smaller than the pivot), (equal to the pivot), (larger than the pivot).
- Recursively sort and .
- Concatenate: sorted- sorted-.
Key Idea
This is the same partition idea as QuickSelect and Partition with Pivot — but where QuickSelect only ever recurses into one side (since it just needs one rank), Quick Sort must recurse into both sides, since every element needs to end up in its correct position, not just the one at rank .
Pseudocode (Mid-Level Implementation)
Algorithm 10 Quick Sort
Input: list to be sorted
Output: sorted list
procedure quickSort()
if then
return
Pick a random index
Partition the list into based on
return
Variables & Data Structures
| Name | Type | Purpose |
|---|---|---|
i | Random index | Selects the pivot for this call |
SL, Sv, SR | Sublists | Elements smaller than, equal to, and greater than the pivot |
L, R | Sorted sublists | The recursively-sorted versions of and |
Helper Functions / Operations Used
- Partition around
a_i— same idea as Partition with Pivot; can be done in place in time. ∘(concatenation) — joins the three pieces back into one list; .
Proof of Correctness
The argument below is added, following the same shape as Merge Sort's proof.
Base case: — the single element is trivially sorted, returned directly.
Inductive Hypothesis: suppose quickSort correctly sorts every list of size .
Inductive Step: for a list of size , partitioning around guarantees every element of is every element of , which is every element of (by construction — that’s what “partition around the pivot” means). Since and (as contains at least the pivot itself), the Inductive Hypothesis guarantees and are correctly sorted versions of and . Concatenating then produces a fully sorted list, since each piece is internally sorted and the three pieces are already correctly ordered relative to each other.
Time & Space Complexity Analysis
Expected Runtime
This averages over every possible pivot rank (each equally likely, since the pivot is chosen uniformly at random): if the pivot lands at rank , the two recursive calls cost and respectively, plus for partitioning.
Important
Like Deterministic Selection’s recurrence, this can’t be solved with the Master Theorem directly — it’s a full-history recurrence (it depends on every smaller subproblem size, not just or a fixed fraction of ). The standard way to close this: guess for a suitable constant , substitute the guess back into the sum, bound using the integral , and verify the resulting expression is for large enough — completing the induction and confirming .
General Case
| Complexity | Notes | |
|---|---|---|
| Time | expected, worst case | Randomized pivot choice makes the worst case unlikely but not impossible |
| Space | expected recursion depth (worst case ) | Partitioning itself can be done in place ( auxiliary), so space is dominated by the call stack |
Best / Worst / Average Case
- Best case: — pivot happens to land near the median every time, giving balanced splits (same shape as Merge Sort’s recursion).
- Worst case: — pivot is repeatedly the min or max (e.g. an already-sorted list paired with unlucky random draws, or a poorly-implemented deterministic pivot rule that an adversary can exploit).
- Average case: — proven via the expected-runtime derivation above.
Drawbacks / Constraints
- worst case, unlike Merge Sort’s guaranteed — see Computer Science Introduction/Algorithms/Divide and Conquer/Merge Sort when a worst-case guarantee matters more than average speed.
- Not stable — the partitioning step can reorder equal elements relative to each other, unlike Merge Sort’s
mergestep, which naturally preserves relative order. - Deterministic pivot rules are riskier — always picking, say, the first element as pivot makes the worst case predictable and exploitable (e.g. by an already-sorted or reverse-sorted input); randomization exists specifically to prevent an adversary from reliably triggering the case.
- Alternatives to consider: Computer Science Introduction/Algorithms/Divide and Conquer/Merge Sort for a guaranteed worst case; Insertion Sort for small subarrays (often used as a cutoff inside real Quick Sort implementations, same as with Merge Sort).