Quick Sort
Quick sort is a divide-and-conquer sorting algorithm that picks a pivot value, partitions the array so everything smaller sits to the pivot's left and everything greater or equal sits to its right, and recurses on the two sides. It averages O(n log n) time, sorts in place, and degrades to O(n²) on adversarial pivots.
Think of the pivot as a customs checkpoint: every element in the range files past once and is placed into either the smaller group or the greater-or-equal group. When the scan ends, the pivot moves into the slot between the two groups and never moves again, while quick sort repeats the process on each side.
How Quick Sort Works
This lesson animates the Lomuto partition: the pivot is always arr[hi], the last value of the current range. Two pointers drive the scan: j checks every element from lo to hi − 1, and i marks the boundary of the smaller-than-pivot zone (the next slot reserved for a small value).
When arr[j] < pivot, the value swaps into slot i and i advances. When arr[j] >= pivot, nothing moves; j walks on and the value stays in the right zone. After the scan, one final swap puts the pivot into slot i, between the two zones, and partition returns that index p. The recursion then sorts arr[lo..p−1] and arr[p+1..hi], excluding the pivot because it is already in its final position.
Because equal-to-pivot values go right and partitioning uses long-distance swaps, quick sort is not stable.
Quick Sort Step by Step
The default input is [38, 12, 56, 7, 12, 41, 23], with two 12s (12a at index 1 and 12b at index 4) so you can watch stability break.
Partition 1, range [0..6], pivot 23. Scan j = 0: 38 ≥ 23, stays right. j = 1: 12a < 23, swaps into slot 0 (i advances to 1). j = 2: 56 stays. j = 3: 7 < 23, swaps into slot 1. j = 4: 12b < 23, swaps into slot 2. j = 5: 41 stays. The final swap moves the pivot into index 3: [12a, 7, 12b, 23, 56, 41, 38]. Index 3 is settled.
Partition 2, the left side [0..2] = [12a, 7, 12b], pivot arr[2] = 12b. Scanning 12a ≥ 12b fails the strict <, so 12a stays in the right zone. Next, 7 < 12b swaps into slot 0. The pivot swap lands 12b at index 1: [7, 12b, 12a, 23, ...]. Now 12b precedes 12a, reversing their original order.
The remaining calls finish quickly: [0..0] and [2..2] are single-element base cases. The right side [4..6] = [56, 41, 38] partitions on pivot 38; neither 56 nor 41 is smaller, so 38 swaps directly to index 4, leaving [38, 41, 56]. Its sub-range [5..6] picks pivot 56, which is already in its final slot. The result is [7, 12b, 12a, 23, 38, 41, 56]: sorted by value, with the two 12s reordered.
In the animation, the zone bands under the row show the partition's state: the left band is the certified < pivot zone, the middle is scanned >= pivot, and the rest is unscanned territory j has not reached. When a pivot snaps into place and turns green, that index is permanently settled.
Time and Space Complexity
| Case | Time | Space |
|---|---|---|
| Best | O(n log n) | O(log n) stack |
| Average | O(n log n) | O(log n) stack |
| Worst | O(n²) | O(n) stack |
The animated version uses plain Lomuto partitioning with a last-element pivot and no randomization so the partition mechanics stay readable. The worst case is any input that repeatedly hands the partition its own minimum or maximum: each level then peels off a single element, which happens on an already-sorted array. Production implementations avoid predictable worst cases with random or median-of-three pivots; the worst-case time bound remains O(n²), while bad inputs stop being predictable.
Edge Cases
- Empty or one-element ranges. These are the base cases where recursion stops, including the empty ranges left behind by a boundary pivot.
- Already sorted or reverse-sorted input. This triggers the worst case for a last-element pivot: maximally unbalanced partitions and O(n²) time.
- Duplicates. Equal values end up in the right positions, though quick sort is not stable; the default input's two 12s come out reordered.
- Values equal to the pivot. Equal values satisfy
>= pivotand pile into the right zone, so an all-equal array is another quadratic input for plain Lomuto.
Common Mistakes
- Recursing on
[lo..p]or[p..hi]. Including the pivot's index re-sorts a settled element and causes infinite recursion. Excludepon both sides. - Forgetting the final pivot swap. Without
arr[i] ↔ arr[hi], the pivot stays parked at the end and the returned index is wrong. - Advancing
ion every comparison.imoves only when a value is accepted into the small zone; it marks that boundary rather than the scan position. - Changing the guard to
<=casually. That relocates every equal-to-pivot value to the left zone, producing a different partition with different balance. - Assuming stability. The values end up sorted, while equal elements can lose their original order. Use merge sort when identity order matters.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Premium players need paid access. Every Explain doc is free.
Embed this lesson in a blog, docs, or course?
Found a mistake or something unclear?