Day 11 — Sorting Algorithms I
Sorting is the unglamorous workhorse of computer science. Almost every interesting algorithm starts with “first, sort the input.” Search becomes O(log n) only after sorting. Two-pointer techniques rely on sorted data. Merge intervals, find duplicates, group anagrams — all begin with a sort.
Today we learn four comparison-based sorts, in order from “humans naturally invented this” to “this is what your standard library actually uses.”
What you’ll learn
- Introduction — the science of sorting: stable vs unstable, in-place vs out-of-place, comparison vs distribution sorts, and the Ω(n log n) lower bound that explains why no comparison sort can beat it.
- Bubble Sort — the simplest sort. O(n²). Almost never the right choice — but a perfect starting point.
- Selection Sort — find the smallest, swap it to the front, repeat. Still O(n²), but does the fewest swaps of any algorithm.
- Insertion Sort — how you sort playing cards. O(n²) worst case, but O(n) on nearly-sorted data — which is why it’s hiding inside every fast sort in production.
- Merge Sort — the first sort that breaks the O(n²) ceiling. Divide and conquer in its purest form, O(n log n) guaranteed.
- Practice Questions — apply what you’ve learned.
Tomorrow (Day 12) we’ll cover the rest: Quick Sort, Heap Sort, and the linear-time non-comparison sorts.
Why bother learning O(n²) sorts?
You’ll never write production code that uses bubble sort. So why study it?
Three reasons:
- Pedagogy. The simple sorts teach you the vocabulary of sorting: comparisons, swaps, in-place rearrangement, stability. Once you’ve internalized those on bubble sort, the harder algorithms are easier to read.
- Insertion sort actually ships. When the input is small (say, fewer than 16 elements) or nearly sorted, insertion sort beats quicksort and merge sort. That’s why Java’s
Arrays.sort, C++‘s introsort, and Python’s Timsort all contain insertion sort as a subroutine. - Interviews ask. “Walk me through bubble sort,” “What’s the difference between selection and insertion?” — these still come up, especially at entry level.
Today is the “simple comparison sort” half. Tomorrow’s Day 12 covers the advanced comparison sorts (Quick, Heap) and the non-comparison sorts (Counting, Radix, Bucket) that can break the O(n log n) lower bound by cheating — using the values as indices.
Ready? Open Introduction to get the vocabulary down, then we’ll start with bubble sort.