Searching and sorting
Overview
Kent Quanrud introduces algorithm analysis through binary representation, binary search, and sorting, showing how structure can turn an apparently large search into logarithmic work. He builds merge sort from a recursive specification, analyzes its O(n log n) running time with a recursion tree, and proves that comparison-based sorting requires Ω(n log n) comparisons using a decision-tree counting argument.
Key takeaways
- Binary search finds a value among n ordered possibilities in O(log n) guesses because each midpoint comparison eliminates about half the remaining candidates.
- Repeatedly selecting the smallest remaining item requires Θ(n²) work, while merging sorted halves takes O(n), enabling merge sort to run in O(n log n).
- A recursive specification states the input-output contract that recursive calls can rely on and that induction uses to establish correctness.
- The merge-sort recurrence T(n) = 2T(n/2) + O(n) has O(n) work per recursion-tree level and about log₂(n) levels, yielding O(n log n).
- In the comparison model, K binary comparisons distinguish at most 2^K cases, while sorting n distinct elements must handle n! orders; thus K ≥ log₂(n!) = Ω(n log n).
- Merge sort’s O(n log n) upper bound matches the comparison-sorting Ω(n log n) lower bound, certifying its asymptotic optimality within that model.
Chapters
- Kent Quanrud connects Ada Lovelace’s interest in Charles Babbage’s mechanical calculations with Alan Turing’s effort to formalize computation.
- He directs students to fundamentalgorithms.com for course materials and describes weekly homework, two midterms, a final, and Tuesday problem-solving sessions.
- Quanrud recommends struggling with homework rather than using chatbots, citing lower midterm scores among chatbot users in the prior semester.
- Ten fingers can represent 10 through unary counting, but treating each finger as an on/off bit gives 2^10 possible patterns.
- The contrast between unary, decimal, and binary representations illustrates how a compact encoding can represent many values.
- Each additional independent bit doubles the number of representable combinations.
- Starting at 50 divides 100 possible secret numbers into two groups, and each over-or-under response eliminates about half.
- Repeatedly guessing the midpoint takes about log₂(100) comparisons, or roughly seven guesses including the final identification.
- Binary search deduces the answer by exploiting order; it does not rely on guessing the secret number directly.
- Quanrud contrasts exponential growth, such as 2^n combinations, with logarithmic growth, which reverses repeated doubling.
- He estimates log₂ of the roughly 10^82 atoms in the universe at about 272, then applies the logarithm again to get about 8.
- The examples frame algorithm analysis around distinguishing combinatorial explosion from problems with exploitable structure.
- The class sorts a list by repeatedly finding the smallest remaining value, the basic idea behind selection sort.
- For n values, the repeated scans examine lists of sizes n, n−1, down to 1, totaling n(n+1)/2 comparisons.
- A quick bound shows the sum is Θ(n²): at least n/2 terms are each at least n/2, so the total is at least n²/4.
- Big-O describes an asymptotic upper bound while setting aside constant factors that depend on the machine, compiler, and definition of a step.
- For selection sort, the important feature is quadratic growth, not whether the implementation takes exactly n² operations or a constant multiple.
- Ignoring low-level constants makes algorithm comparisons more portable and focuses attention on the dominant growth rate.
- A sorted list can be verified by checking its n−1 adjacent pairs, since transitivity ensures the entire sequence is ordered.
- To merge two sorted halves, compare their first unprocessed elements and append the smaller one, advancing that list’s pointer.
- Merging takes O(n) time; any elements left when one list runs out must be copied into the result.
- Merge sort recursively sorts the first half and second half of an array, then merges the two sorted results.
- The merge routine supplies the key subproblem operation: two sorted lists become one sorted list in linear time.
- The recursive design replaces selection sort’s repeated full scans with progressively smaller subproblems.
- Quanrud asks students to state a recursive specification by naming the procedure and making its input and output explicit.
- For merge sort, the contract is to take an array of comparable numbers and return those values in sorted order.
- The contract lets each recursive call be treated as a reliable operation and provides the inductive hypothesis for correctness proofs.
- The pseudocode handles a small-array base case, finds the midpoint, recursively sorts both halves, and merges their results.
- Quanrud uses one-based indexing in the presentation and notes that zero-based indexing is also valid if specified consistently.
- Correctness follows by induction: the base case is sorted, both smaller halves are sorted by induction, and merging preserves sorted order.
- Define T(n) as an upper bound on the work for an input of size n, then derive a recurrence from the pseudocode.
- Two recursive calls process inputs of about n/2, while splitting and merging contribute O(n) additional work.
- The resulting recurrence is T(n) = 2T(n/2) + O(n); floor and ceiling details do not change the asymptotic result.
- At depth i, the recursion tree has 2^i subproblems of size n/2^i.
- Each level performs O(n) total merge work because 2^i subproblems each contribute work proportional to n/2^i.
- There are about log₂(n) levels before the subproblems reach size 1, yielding O(n log n) total work.
- Quanrud shifts from proving that merge sort is fast to asking whether any correct sorting algorithm can be faster.
- A lower bound of Ω(n) is immediate because an algorithm must inspect the input, but it does not close the gap to merge sort’s n log n.
- The comparison model abstracts algorithms to comparisons between pairs of elements, allowing a claim about all algorithms in that model.
- If an algorithm makes K comparisons, each with two possible outcomes, its comparison transcript can encode at most 2^K outcomes.
- Sorting n distinct values must distinguish among n! possible input orderings, so a correct algorithm needs 2^K ≥ n!.
- Taking logarithms gives K ≥ log₂(n!), establishing a lower bound without examining any particular sorting implementation.
- At least n/2 factors in n! are at least n/2, giving n! ≥ (n/2)^(n/2) and therefore log₂(n!) = Ω(n log n).
- Together with merge sort’s O(n log n) upper bound, the lower bound proves that comparison-based sorting is asymptotically optimal.
- Quanrud introduces inversion counting as a practice problem: count out-of-order pairs and consider exploiting already-sorted halves to improve on O(n²).
Transcript excerpt
0:05 Well, here's sort of the tacky way I start every every semester. Uh, here are kind of two of the original people behind theoretical computer science. And maybe you recognize them. Uh, who's on the left? >> Lovely. >> Ada Love Lace. Yeah. And who's on the right? >> Um, Turing. So, Turing probably you've heard plenty of, right? Uh so he was trying to solve this very interesting question of basically does every question more or less have an algorithm or something computational. They didn't really know what they meant by algorithm or computation or whatever it is but this was just posed as like a mathematical logical question. And in the course of trying to solve uh this question which he was sort of inspired by this uh famous diagonalization proof
0:53 of this guy named Cantor. It doesn't quite matter if you haven't heard of him but uh he essentially to get his proof started had to propose some kind of abstract concept that looks a lot like a modern computer right and at some level just to define this question of can you sort of solve anything with mechanical steps he had to really dig into what it means to solve things with mechanical steps and that design more or less is the modern computer that's what John von based it on a love lace must be 150 years before anyway some time earlier and or no less less than 100 years earlier but still she uh there's this crazy guy named Babage uh who was trying to build something like a calculator out of steam and back then they just wanted to build like logarithm tables so you know where like
1:40 cannonballs land or something depending on the angle something very primitive uh uh but she uh encountered this guy
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Kent Quanrud.