Quicksort is the sorting algorithm most language standard libraries have historically defaulted to (C's
qsort, and the core of many hybrid sorts), because its expected running time is with a small constant and it sorts in place — yet its worst-case running time is , and that worst case is triggered by exactly the kind of already-sorted or adversarially-crafted input a naive deployment might see. This note derives both bounds, shows precisely how randomization — first introduced as a design principle in Topic 1 — converts the dangerous worst case into a vanishingly unlikely event regardless of the input, and proves the expected bound using the same indicator-variable technique built there.In this lesson
- Learning objectives
- Quicksort and partitioning
- The worst case:
- The best case:
- Randomized quicksort
Before you start
- Divide-and-Conquer, Recurrences, and the Master Theorem
- Probabilistic Analysis and Randomized Algorithms
The full lesson, with worked examples and exercise solutions, is for members.
Read the full lessonNew to SelfMadeQuant.com? See membership options or explore the curriculum.