What's happening?
- Take the last value of the range as the pivot.
- Walk through the range: each value smaller than the pivot is swapped into the growing "smaller" side.
- Swap the pivot just after the smaller side — it is now final — and repeat on the left and right parts.
Complexity
- Time
- O(n log n) on average · O(n²) worst case (bad pivots)
- Space
- O(log n) for the recursion
Where you'll meet it
The default in-place sort in many libraries (C's qsort, Java's sort for primitives uses a dual-pivot variant) because it is fast in practice and cache-friendly.
Common mistake
Always taking the last value as pivot on already-sorted data: every split is lopsided and it degrades to O(n²). Real implementations pick random or median-of-three pivots.
FAQ
What is the pivot?
The value the range is split around. After partitioning it sits exactly where it belongs in the sorted list.
Why is the worst case O(n²)?
If the pivot is always the smallest or largest value, each step removes only one value instead of halving the range.
Is quick sort stable?
No — partition swaps can reorder equal values.