Kayavak / Foundations of Computing / CS·04 Algorithms / Deeper
Deeper · Foundations of Computing
Algorithms
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Algorithms are far older than computers. Euclid's method for the greatest common divisor (around 300 BCE) is still taught unchanged, and the word itself comes from the name of the ninth-century Persian mathematician Muhammad ibn Musa al-Khwarizmi, whose book on calculating with Hindu–Arabic numerals spread step-by-step arithmetic through Europe.
What computers added was the need to compare procedures. A machine that does millions of steps a second makes the difference between n² and n log n the difference between a second and a week. John von Neumann wrote merge sort for the EDVAC in 1945; Tony Hoare invented quicksort around 1959–60 while working on machine translation; Edsger Dijkstra found his shortest-path algorithm in 1956 (published 1959), reportedly in about twenty minutes at a café. Donald Knuth's The Art of Computer Programming (from 1968) turned the analysis of algorithms into a discipline, and in 1976 he fixed the meanings of O, Ω and Θ that computer scientists use, borrowing notation from Paul Bachmann (1894) and Edmund Landau.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
An algorithm is a procedure that's correct for every valid input and finishes. We compare algorithms by how their cost grows as the input grows, because growth eventually dominates everything else.
| Question | Answer |
|---|---|
| What is it? | A finite, precise procedure from inputs to outputs |
| Problem it solves | Getting the right answer within the time and memory you have |
| Parts | A representation of the input (a data structure), steps, and an argument that it's correct (an invariant) |
| How it works | Strategies recur: search, divide and conquer, dynamic programming, greedy choice, graph traversal |
| Assumptions | Big-O assumes large inputs and counts steps as equal in cost |
| When they change | Small inputs, caches and constant factors can reverse the ranking (insertion sort wins under ~20 items) |
| Where it fails | Problems with no known efficient algorithm (NP-hard), input that doesn't fit in memory, inputs chosen to hit the worst case |
| Related ideas | Data structures (ch03) make steps cheap; recurrences and induction (ch05) analyse and prove them |
What if?
What if comparing two items were free, but moving one were expensive?
Reason it through, then open
Then the number of moves would matter, not comparisons. Selection sort, which looks slow (n² comparisons), does at most n − 1 swaps, so it could beat merge sort, which moves every element on every level. This is a real situation for very large records on slow storage, and it's why real systems often sort small keys with pointers to the records instead of the records themselves.
What if your input is already almost sorted?
Reason it through, then open
Insertion sort does only as much work as there are out-of-place pairs, so nearly sorted input costs it close to O(n). A naive quicksort that always picks the first element as pivot does the opposite: sorted input makes every split lopsided and it degrades to O(n²). Timsort, used for objects in Python and Java, looks for already-sorted runs and merges them, so it's fast on exactly this kind of real-world input.
Where the boundary is
| Aspect | Big-O complexity | Actual running time |
|---|---|---|
| Measures | How cost grows with input size | Seconds on a machine, for one input |
| Ignores | Constant factors, caches, the hardware | Nothing: it's what you feel |
| Good for | Predicting what happens when input grows 100× | Deciding between two algorithms at today's size |
| Misleads when | Inputs are small, or constants differ wildly | You measure one size and extrapolate |
Derive it
Why no comparison sort can beat n log n.
- A comparison sort learns about the input only by asking "is a < b?". Draw every possible run as a binary tree: each inner node is a comparison, each leaf an output order.
- The input could be any of the n! orderings of n distinct items, and each needs a different leaf, so the tree has at least n! leaves.
- A binary tree of height h has at most 2ʰ leaves, so 2ʰ ≥ n!, which gives h ≥ log₂(n!).
- The height is the worst-case number of comparisons. And log₂(n!) = log₂ 1 + log₂ 2 + … + log₂ n; the last n/2 terms are each at least log₂(n/2), so the sum is at least (n/2) log₂(n/2), which grows like n log n.
- So every comparison sort needs Ω(n log n) comparisons in the worst case. Counting and radix sort escape the bound by not comparing: they use the keys' digits as addresses.
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Comparison sorting needs Ω(n log n) comparisons | Theorem | The decision-tree argument above |
| Quicksort averages O(n log n) | Theorem | For random pivots or random input order |
| Insertion sort beats merge sort on tiny inputs | Measured | Why library sorts switch to it below a cutoff (the chapter's lab) |
| O(n²) on 10⁵ items is too slow for interactive use | Model | Rule of thumb from ~10⁸–10⁹ simple steps per second |
Still open
Whether P equals NP, whether every problem whose answer is quick to check is also quick to find, is the best known open question in computer science and one of the Clay Millennium Prize problems. Most researchers expect P ≠ NP; nobody has proved it.
What would change your mind? A practical polynomial-time algorithm for an NP-complete problem such as Boolean satisfiability would overturn the assumption, used everywhere from cryptography to scheduling, that these problems are inherently hard.
Recall
Why can't any comparison sort beat n log n in the worst case?
It must distinguish all n! input orders, and a binary decision tree with n! leaves has height at least log₂(n!), which grows like n log n.
How do counting and radix sort beat n log n?
They don't compare; they use key digits as array positions.
Why might an O(n²) algorithm beat an O(n log n) one?
For small inputs, smaller constant factors and better cache behaviour win.
Why does a first-element pivot make quicksort slow on sorted input?
Every partition is maximally lopsided, so there are n levels of nearly n work: O(n²).
What's the open question P vs NP asking?
Whether every problem whose solutions can be checked quickly can also be solved quickly.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.