Overview

Quicksort is a highly efficient comparison-based sorting algorithm that uses the divide-and-conquer paradigm. Despite having a worst-case running time of Θ(n²), it is often the practical sorting algorithm of choice because its expected running time is Θ(n lg n) with very small constant factors. It also sorts in place (requiring only O(lg n) additional stack space) and performs well in virtual-memory environments.

The chapter covers the core algorithm and its PARTITION subroutine (Section 7.1), intuitive and formal performance analysis for worst-case, best-case, and average-case scenarios (Sections 7.2 and 7.4), and a randomized variant that eliminates dependence on input ordering (Section 7.3).

Key Concepts

  1. A[p..i] contains elements ≤ x (the pivot).

  2. A[i+1..j−1] contains elements > x.

  3. A[r] = x (the pivot itself).

This invariant is proved via initialization, maintenance, and termination arguments.

Algorithms and Techniques

QUICKSORT(A, p, r)

The main recursive procedure:

  1. Base case: If p ≥ r, the subarray has zero or one element — do nothing.

  2. Partition: Call PARTITION(A, p, r) to get pivot index q, placing A[q] in its final sorted position.

  3. Recurse: Recursively sort A[p..q−1] (elements ≤ pivot) and A[q+1..r] (elements > pivot).