Three-Way Quick Sort
Partition values into less-than, equal-to, and greater-than regions so duplicate pivots finish together.
Core idea
Maintain three regions around one pivot: values below it, values equal to it, and values above it. A single scan expands the regions until no unknown values remain.
Read the visualization
The lt, i, and gt pointers bound the three colored regions. Less and greater branches swap values across a boundary, while equal values simply advance the scan.
Take the next unsorted interval from the explicit work stack.
Complexity and tradeoffs
Time: Average O(n log n); worst O(n^2). Space: Average O(log n). Three-way partitioning is especially effective when many input values are equal.
Where it fits
Three-way partitioning is the preferred Quick Sort variant for data with many duplicate keys. Distinct random data behaves similarly to ordinary Quick Sort.