Dual-Pivot Quick Sort
Choose two pivots and partition one interval into three regions during a single scan.
Core idea
Order two endpoint pivots p and q, then divide the scan into values below p, between p and q, and above q. Placing both pivots finishes three subproblems at once.
Read the visualization
Two pivot markers remain visible while lt, i, and gt move the unknown region inward. Each comparison selects one of the three partition branches.
Take the next unsorted interval from the work stack.
Complexity and tradeoffs
Time: Average O(n log n); worst O(n^2). Space: Average O(log n). Good partitions reduce recursion depth and can improve cache behavior, but the worst case remains quadratic.
Where it fits
Dual-pivot partitioning can reduce comparisons and improve cache behavior in tuned primitive-array sorts. Pivot selection remains essential to avoid unbalanced recursion.