Skip to content
Data Structures and Algorithms / Linear Structures, Hashing, and the Sorting Toolkit

Quicksort and Randomized Quicksort

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 Θ(nlog⁡n)\Theta(n\log n) with a small constant and it sorts in place — yet its worst-case running time is Θ(n2)\Theta(n^2), 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 O(nlog⁡n)O(n\log n) expected bound using the same indicator-variable technique built there.

In this lesson

  1. Learning objectives
  2. Quicksort and partitioning
  3. The worst case: Θ(n2)\Theta(n^2)
  4. The best case: Θ(nlog⁡n)\Theta(n\log n)
  5. Randomized quicksort

Before you start

The full lesson, with worked examples and exercise solutions, is for members.

Read the full lesson

New to SelfMadeQuant.com? See membership options or explore the curriculum.