Loading…
Loading…
Charting your lesson…
Opening your lesson…
Quick Sort and Partitioning
Lesson 3 of 7
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
Quicksort is the fastest general-purpose sort in practice, the default in most standard libraries, and its partition step is a reusable powerhouse (quickselect, Dutch flag). Its catch — an O(n²) worst case — is exactly what interviewers probe.
Core idea: pick a pivot, partition the array so smaller elements go left and larger go right (the pivot lands in its final sorted position), then recursively sort the two sides. Partitioning is O(n), and good pivots give log n depth → O(n log n) average.
Step 2/ 61 min
def quickSort(a, lo, hi):
if lo < hi:
p = partition(a, lo, hi) # pivot now in final place
quickSort(a, lo, p-1)
quickSort(a, p+1, hi)
Lomuto partition: keep a boundary i; scan, swapping elements ≤ pivot to the left region; finally place the pivot at i. One O(n) pass.
std::sort) starts with quicksort and switches to heapsort when recursion gets too deep, guaranteeing O(n log n) worst case.Step 3/ 61 min
Partition [7, 2, 1, 6, 8, 5, 3, 4] with pivot 4 (last). Scan, moving elements < 4 to the left: 2,1,3 go left of the boundary; result places 4 so that [2,1,3] | 4 | [6,8,5,7]. Pivot 4 is now in its final sorted index; recurse on each side. Each partition is O(n); balanced splits give O(n log n).
Step 4/ 61 min
Step 5/ 61 min
Using the first or last element as the pivot and then sorting an already-sorted (or reverse-sorted) array — this produces maximally unbalanced partitions and O(n²) time. Randomise the pivot or use median-of-three to keep expected O(n log n).
Step 6/ 6· Worked examples1 min
Problem
Apply one Lomuto partition to [7, 2, 1, 6, 8, 5, 3, 4] using the last element (4) as pivot. Where does the pivot end up?
Try it on paper first.
Recall
Keep this
Usage: randomise or use median-of-three pivots to avoid the O(n²) worst case.
Quicksort is the fastest general-purpose in-place sort in practice, averaging O(n log n) with excellent cache behaviour, and its partitioning routine is the engine behind Quickselect (kth element) and Dutch-flag problems. The catch interviewers probe: a bad pivot gives O(n²).
Pick a pivot, partition the array so smaller elements go left and larger go right (the pivot lands in its final position), then recursively sort each side. Average O(n log n); worst O(n²) when pivots are consistently extreme (e.g., already-sorted input with a first/last pivot). Randomised or median-of-three pivots make the worst case improbable. It's in-place (O(log n) recursion stack) but not stable. Partition schemes: Lomuto (simpler) and Hoare (fewer swaps).
| Aspect | Value |
|---|---|
| Average time | O(n log n) |
| Worst time | O(n²) |
| Space | O(log n) stack |
| Stable | No |
| Pivot fix | random / median-of-three |
Implement quicksort/partition, Quickselect for kth largest, sort colors (Dutch flag), or "why can quicksort be O(n²)?". Interviewers grade the partition logic, pivot strategy, and complexity nuance.
Quicksort partitions around a pivot and recurses — average O(n log n), worst O(n²) on bad pivots (fix with randomisation/median-of-three). In-place, O(log n) stack, not stable. Its partition powers Quickselect (O(n) average kth element).
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.