Quick sort: partitioning and pivots
Quick sort picks a pivot, partitions smaller values left and larger right, then recurses on both sides. It is fast in practice but O(n²) with bad pivots.
Before you start: Merge sort, Recursion: base case, recursive case, the call stack.
The idea
To sort a pile fast, pick one value as the pivot. Smaller values go to its left and larger ones to its right.
One pass puts the pivot in its final spot. The two sides are then smaller sorting problems of the same kind.
Intuition
After one pass, 4 sits where it belongs. Everything on its left is smaller, everything on its right is larger.
Sort each side the same way and the whole array is sorted. The pivot never moves again.
This is recursion. The same method runs on a smaller range until one value is left, and one value is already sorted.
Complexity
Even splits give about n log n steps. A lopsided split at every level costs n².
Each level of recursion touches every value once. Even splits need about log n levels. Lopsided splits need n levels.
Values move inside the one array, so the extra space is only the call stack. That is about log n frames with even splits.
Pitfalls
On sorted input the last value is the largest. Each pass peels off one value, so the cost grows to n².
With all values equal, none is smaller than the pivot. The split is lopsided again.
Choosing the pivot by position cannot see patterns in the data. Random or median-of-three pivots avoid most bad cases.
Variations
Hoare's scheme starts i and j at both ends and walks them inward. On this array it needs 2 swaps where Lomuto needed 3.
With many equal values, split into smaller, equal and larger bands. Equal values then never move again.
Code
Pseudocode
quicksort(a, lo, hi)
if lo ≥ hi: return
p ← partition(a, lo, hi)
quicksort(a, lo, p - 1)
quicksort(a, p + 1, hi)
partition(a, lo, hi)
pivot ← a[hi]
i ← lo
for j ← lo to hi - 1
if a[j] < pivot
swap a[i], a[j]
i ← i + 1
swap a[i], a[hi]
return i
Python
def quicksort(a, lo, hi):
if lo >= hi:
return
p = partition(a, lo, hi)
quicksort(a, lo, p - 1)
quicksort(a, p + 1, hi)
def partition(a, lo, hi):
pivot = a[hi]
i = lo
for j in range(lo, hi):
if a[j] < pivot:
a[i], a[j] = a[j], a[i]
i += 1
a[i], a[hi] = a[hi], a[i]
return i
JavaScript
function quicksort(a, lo, hi) {
if (lo >= hi) return;
const p = partition(a, lo, hi);
quicksort(a, lo, p - 1);
quicksort(a, p + 1, hi);
}
function partition(a, lo, hi) {
const pivot = a[hi];
let i = lo;
for (let j = lo; j < hi; j++) {
if (a[j] < pivot) {
[a[i], a[j]] = [a[j], a[i]];
i++;
}
}
[a[i], a[hi]] = [a[hi], a[i]];
return i;
}
Java
static void quicksort(int[] a, int lo, int hi) {
if (lo >= hi) return;
int p = partition(a, lo, hi);
quicksort(a, lo, p - 1);
quicksort(a, p + 1, hi);
}
static int partition(int[] a, int lo, int hi) {
int pivot = a[hi];
int i = lo;
for (int j = lo; j < hi; j++) {
if (a[j] < pivot) {
int tmp = a[i]; a[i] = a[j]; a[j] = tmp;
i++;
}
}
int tmp = a[i]; a[i] = a[hi]; a[hi] = tmp;
return i;
}
Cost at a glance
| Best | O(n log n) |
|---|---|
| Average | O(n log n) |
| Worst | O(n²) |
| Space | O(log n) |
| Stable | no |
| In-place | yes |
| Adaptive | no |
Practice problems (7)
Partition by hand Warm-up
Follow the Lomuto partition on [8, 3, 9, 1, 6, 2, 7, 5]. The pivot is the last value, 5. Before each swap plays, arrange the values as they look afterwards.
Pivot that causes O(n²) Warm-up
Quick sort uses the last value as the pivot. Which input makes it do the most work?
Fill in the Lomuto partition Warm-up
Complete the Lomuto partition. It uses the last value as the pivot, moves smaller values to the front and returns the pivot's final index.
Fill the three blanks.
Implement quick sort with Lomuto Standard
Sort nums in ascending order with quick sort. Use the Lomuto partition: the last value is the pivot, and a boundary index i collects the smaller values at the front.
Sort in place and return the same array.
Implement quick sort with Hoare Standard
Sort nums in ascending order with quick sort and the Hoare partition. The pivot is the first value. i walks right and j walks left, and out-of-place pairs swap until the pointers meet.
Sort in place and return the same array.
Spot the bug: the scan stops early Standard
This quick sort uses the Lomuto partition but returns unsorted arrays for some inputs. Click the line with the bug, then fix it.
Sort colors (Dutch national flag) Challenge
The array nums holds only 0, 1 and 2 (red, white, blue). Sort it in place in one pass so all 0s come first, then all 1s, then all 2s. Do not use a library sort.
Return the same array.
Next on the path: Counting sort