Efficient Sorting Algorithms
Lesson 7 of 20Watch a pivot land in its final home and eight bars split, zip and merge into O(n log n) order.
Divide and conquer
When n hits a million, O(n²) sorting needs a trillion comparisons. Divide and conquer gets there in about twenty million — split, solve the halves, zip them back.
n work × log n levels
Halving the problem log₂n times, with a linear pass at each level, gives O(n log n): 1,000,000 items take ~20 million steps instead of a trillion.
Merge sort
Split down to singles, then zip sorted halves together. Guaranteed O(n log n) and stable — but it pays O(n) auxiliary memory for the merge.
Quick sort
Pick a pivot, partition so everything ≤ pivot sits left and everything > pivot sits right, then recurse on both sides. In-place and cache-friendly.
Lomuto partition
One pass: i guards the ≤-zone, j scans. Each a[j] ≤ pivot grows the zone with a swap; one final swap drops the pivot between the zones — its forever home.
The catch
Quick sort degrades to O(n²) on sorted input with a last-element pivot; merge sort never degrades. std::sort is introsort: quicksort with a heapsort fallback.
Partition in motion
Lomuto partition on six bars — pivot 5 slides into its final home in one linear pass.
Lomuto partition: pivot = last bar (5). i guards the ≤-zone — empty for now (i = -1) — while j scans left to right.
The merge zip
The merge is the whole trick — two sorted halves zip into one in a single linear pass.
Walkthrough
Full Lomuto quicksort on {7, 2, 9, 4, 6}; then the merge pass that stitches two pre-sorted halves with a stable tie rule.
1#include <iostream>2using namespace std;34void quickSort(int a[], int lo, int hi) {5if (lo >= hi) return;6int pivot = a[hi], i = lo - 1;7for (int j = lo; j < hi; j++)8if (a[j] <= pivot) {9i++;10int t = a[i]; a[i] = a[j]; a[j] = t;11}12int t = a[i + 1]; a[i + 1] = a[hi]; a[hi] = t;13quickSort(a, lo, i);14quickSort(a, i + 2, hi);15}1617int main() {18int a[] = {7, 2, 9, 4, 6};19quickSort(a, 0, 4);20for (int k = 0; k < 5; k++) cout << a[k] << " ";21return 0;22}
a = {7, 2, 9, 4, 6} — five values for Lomuto quicksort.
Merge recursion tree
Eight values split down to singles, then zip back up — three merge levels, O(n log n).
Merge sort splits first and asks questions later. Eight unsorted values sit at the root; the plan is split to singles, then zip back up level by level.
Predict the output
Trace a Lomuto partition by hand — the pivot 4 wants to know its index.
1#include <iostream>2using namespace std;34int main() {5int a[] = {9, 3, 8, 1, 6, 4};6int n = 6, pivot = a[5];7int i = -1;8for (int j = 0; j < n - 1; j++)9if (a[j] <= pivot) {10i++;11int t = a[i]; a[i] = a[j]; a[j] = t;12}13int t = a[i + 1]; a[i + 1] = a[5]; a[5] = t;14cout << "pivot at index " << i + 1 << endl;15for (int k = 0; k < n; k++) cout << a[k] << " ";16return 0;17}
Hint: i counts values ≤ the pivot 4. The 3 and the 1 grow the zone with swaps; 9, 8 and 6 are skipped. The final swap drops 4 between the zones.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
Merge sort: split to singles, zip sorted halves — guaranteed O(n log n) and stable.
Merging is one linear pass; the whole cost is log n levels of n work.
Merge sort pays O(n) auxiliary memory; quick sort partitions in place with O(log n) stack.
Lomuto partition: i guards the ≤-zone, j scans; each a[j] ≤ pivot grows the zone with one swap, then the pivot drops between the zones.
Quick sort averages O(n log n) but hits O(n²) on adversarial pivots — randomize the pivot or use median-of-three.
std::sort is introsort (quicksort + heapsort fallback + insertion for tiny runs); std::stable_sort is merge sort.