Efficient Sorting Algorithms
Lesson 7 of 20Split [9,4,7,1,8,5] down to atoms, merge it back sorted — and watch a pivot re-partition the array around itself.
Divide and conquer
The O(n²) basics grow a sorted region one element at a time. The fast sorts split the whole problem in half — and halving is what turns quadratic into n log n.
Merge sort: guaranteed
Split to single elements, merge sorted halves upward. O(n log n) on every input, stable — but it needs an O(n) auxiliary array.
Quicksort: pivot rule
Partition around a pivot, park it, recurse on both sides. Average O(n log n) in-place with tiny constants; worst case O(n²).
Lomuto partition
Pivot = last element. i banks everything ≤ pivot on the left; the pivot finally lands at p = i + 1, done forever.
Java ships both ideas
Arrays.sort(int[]) runs dual-pivot quicksort on primitives; object arrays get TimSort — a stable merge-sort hybrid.
The halving math
log₂(1,000,000) ≈ 20 levels. A million elements sort in about 20 rounds of linear work, not a million sweeps.
Partition in motion
One Lomuto partition on [7, 2, 8, 5, 3, 6] — the pivot 6 ends up exactly where it belongs.
Partition around the last element: pivot = 6. i banks everything ≤ 6 on the left; j does the scanning.
Quicksort flow
Each call partitions its slice, parks one pivot forever, then recurses on both sides.
Walkthrough
Quicksort partitioning first, then merge sort zipping two sorted halves — both print via Arrays.toString.
1import java.util.Arrays;23public class QuickSort {4static int partition(int[] a, int lo, int hi) {5int pivot = a[hi];6int i = lo - 1;7for (int j = lo; j < hi; j++) {8if (a[j] <= pivot) {9int t = a[++i]; a[i] = a[j]; a[j] = t;10}11}12int t = a[i + 1]; a[i + 1] = a[hi]; a[hi] = t;13return i + 1;14}15static void quickSort(int[] a, int lo, int hi) {16if (lo >= hi) return;17int p = partition(a, lo, hi);18quickSort(a, lo, p - 1);19quickSort(a, p + 1, hi);20}21public static void main(String[] args) {22int[] a = {7, 2, 8, 5, 3, 6};23quickSort(a, 0, 5);24System.out.println(Arrays.toString(a));25}26}
a gets 6 unsorted values.
Merge tree
The full recursion of [9, 4, 7, 1, 8, 5] — splits dive down, merges climb back up.
Six elements walk in: [9, 4, 7, 1, 8, 5]. Merge sort splits first and sorts later.
Predict the output
Run one Lomuto partition by hand — track i, then compute where the pivot parks.
1public class Predict {2public static void main(String[] args) {3int[] a = {4, 9, 2, 7, 1, 5};4int pivot = a[5], i = -1;5for (int j = 0; j < 5; j++) {6if (a[j] <= pivot) {7int t = a[++i]; a[i] = a[j]; a[j] = t;8}9}10int t = a[i + 1]; a[i + 1] = a[5]; a[5] = t;11System.out.println("p = " + (i + 1));12System.out.println(java.util.Arrays.toString(a));13}14}
Hint: i counts the values ≤ 5 banked on the left; p = i + 1 is where the pivot lands. Watch the swaps shuffle 9 and 1 around.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
Merge sort is O(n log n) on every input and stable — the price is an O(n) auxiliary array.
Quicksort partitions around a pivot: everything ≤ pivot goes left, the pivot parks at p = i + 1.
Lomuto partition: pivot = last element, i banks the small values, one final swap seats the pivot.
Quicksort averages O(n log n) in-place — but a lazy pivot on sorted input degrades it to O(n²).
Arrays.sort(int[]) runs dual-pivot quicksort; object arrays get TimSort, a stable merge-sort hybrid.
Halving the range gives about log₂n recursion levels — 1,000,000 elements need roughly 20.