1. The comparison model
The sorts in this lesson learn about the input only by comparing two elements. That restriction is what makes the n log n lower bound apply. If you know more — for example, that the keys are small integers — you can sort faster with counting or radix sort, which are not comparison sorts.
A comparison sort is stable if equal keys keep their original relative order, and in-place if it uses O(1) extra memory. Those two properties often matter more than the asymptotic constant when you are sorting records with a secondary key.
2. Quadratic sorts
Bubble sort repeatedly swaps adjacent out-of-order pairs. Insertion sort grows a sorted prefix and inserts each new element into it. Both are O(n²) worst case, both are stable, and both are excellent teaching tools. Insertion sort is also genuinely useful for small arrays and nearly sorted data, where it is O(n + inversions).
- Bubble — simple, stable, in-place; almost never the right production choice.
- Insertion — simple, stable, in-place; good for small or nearly sorted inputs.
- Selection — O(n²), not stable in the usual array implementation, but only O(n) swaps.
3. Merge sort
Merge sort splits the array in half, sorts each half, and merges the two sorted halves. The merge is the important part: two pointers walk the halves and repeatedly take the smaller front element. The recursion gives the n log n recurrence.
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) result.push(left[i++]);
else result.push(right[j++]);
}
// append the remaining tailMerge sort is stable if the merge takes the left element on ties. It needs O(n) extra memory, which is its main practical cost. The recursion tree has log n levels and each level merges n elements.
Example: merge sort [5, 2, 4, 1]
Split into [5, 2] and [4, 1]. Sort each half: [2, 5] and [1, 4].
Merge: compare 2 and 1, take 1; compare 2 and 4, take 2; compare 5 and 4, take 4; take the remaining 5. Result: [1, 2, 4, 5].
4. Quicksort
Quicksort chooses a pivot, partitions the array so smaller values are on the left and larger values on the right, then recurses on the two sides. The pivot is in its final position after the partition. The average case is O(n log n); the worst case is O(n²) when the pivot is consistently the smallest or largest element.
pivot = a[hi];
store = lo;
for (j = lo; j < hi; j++) {
if (a[j] < pivot) swap(a[store++], a[j]);
}
swap(a[store], a[hi]);
// recurse on [lo, store-1] and [store+1, hi]Randomized pivots make the bad case unlikely on ordinary inputs, but they do not remove it from the worst-case analysis. Quicksort is in-place and usually faster than merge sort in practice because it has better locality and no auxiliary array.
5. Stability & the lower bound
Merge sort is stable; the usual in-place quicksort is not. If you sort employees by name and then by department, a stable sort keeps the name order within each department. An unstable sort can scramble it.
Any comparison sort must make Ω(n log n) comparisons in the worst case. The argument is information-theoretic: n! possible orders, each comparison gives one bit, so at least log₂(n!) = Θ(n log n) comparisons are needed. Merge sort and heapsort meet the bound; quicksort meets it on average.
What to use
- General-purpose — a library sort (Timsort, introsort, pdqsort) that combines the strengths of insertion, merge, and quick sort.
- Stability required — merge sort or Timsort.
- Memory constrained — heapsort or an in-place quicksort.
- Small or nearly sorted — insertion sort.
6. Practice
Why is merge sort O(n log n) while bubble sort is O(n²)?
When does quicksort hit its O(n²) worst case?
Is merge sort stable? Is quicksort?
Why can't a comparison sort beat Ω(n log n) in the worst case?
7. Resources
Run the sorting playground with the same array size and compare the counters. Bubble and insertion will separate quickly; merge and quick will stay close until the input gets large. Then try an already sorted array and watch quicksort's pivot choice matter.
Continue the track
Open the playground for this topic
Sorting Playground →Run bubble, insertion, merge, and quick sort on the same array and watch the comparison and swap counters diverge.