Unit 2 of 6 · about 5 hours · derive, implement
Divide and conquer, recurrences and partition-based selection
Divide and conquer sets the price of every recursive algorithm you will write. Selection shows the same recurrence idea deciding between linear and quadratic time.
You'll be able to
- Solve , , and with recursion trees, stating depth, per-level cost and leaf count.
- Explain why floors and ceilings change the answer by at most a constant factor, and when an unbalanced split breaks the tree bound.
- Implement deterministic linear-time selection by median of medians and check it against a sorted oracle with pytest on small shuffled inputs.
- State the quickselect recurrence and its quadratic worst case, and point to the partition unit for the invariant and to the randomized unit for the expected cost.
Covers
Read
In this order, only the parts listed
0 of 2 done
- Recursion, Chapter 1Algorithms - recursion, backtracking and greedy
1.4 Mergesort, 1.5 Quicksort, 1.6 The Pattern and 1.7 Recursion Trees (printed pp. 26-35), then 1.8 Linear-Time Selection: Quickselect, Good pivots and Analysis (printed pp. 35-40)
Skip Fast Multiplication and Exponentiation here. They return with dynamic programming and number theory.
- Recursion patterns: linear, tree, and divide-and-conquerDSA Handbook - recursion, backtracking and greedy
What "shape of the recursion" means; Linear: one self-call per frame; Tree: two self-calls per frame; Divide-and-conquer: split, recurse, combine; What it actually costs
Use it as the shape vocabulary for the tree accounting in Erickson. Memoization detail belongs to the dynamic programming stage.
Focus on
- Tree accounting has three numbers: depth, cost per level, and leaf count. with gives depth 10 and 11264 at , checked with a five-line evaluator.
- One-sided recursion is a path, not a tree: sums to , while sums to . Dropping the combine cost changes the answer.
- Groups of five guarantee a pivot that discards a fixed fraction on both sides, giving below , which sums to linear because 1/5 plus 7/10 is below 1.
- Fixed-pivot quickselect on sorted input recurses on each level and is quadratic. The randomness or the median of medians is doing the work, not the partition loop.
Common mistakes · 3
- Reading as . Halving with linear combine is a geometric series summing below ; the log appears only when both halves are solved.
- Applying tree intuition to unbalanced splits without checking: is still , but is quadratic.
- Quoting linear quickselect without naming the pivot policy. First-element pivot on sorted input is the quadratic case, and this unit proves the recurrence rather than the partition code.
Try it yourself
Closed-book, before checking anything
sorted(xs)[k] with pytest on 300 seeded lists of length 1 to 60 with duplicates. Finally count comparisons for first-element-pivot quickselect ( per partition) on sorted range(1000) with : 499500, which is .Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Solve with .Answer
. Level costs are , , and so on, summing below . The recursion is a single path, so there is no leaf multiplicity.
2.Give the quickselect worst case: input, pivot rule, and comparison count.Answer
Sorted input with a first-element pivot and the largest element as the target. Every partition splits into 0 and , so comparisons total : 499500 for . A middle target is still quadratic, about .
3.Why is median-of-medians linear?Answer
Its recurrence is below . The subproblem fractions sum to 9/10, below 1, so the level costs form a convergent geometric series.
4.What does this unit own that the partition unit does not, and vice versa?Answer
This unit owns recurrence analysis and deterministic median of medians. The partition unit owns the partition invariant, the quickselect implementation, adversarial pivots, and the top-k tradeoff.
Practice
- LeetCode 493: Reverse Pairs
Attempt cold with merge-sort counting. Examples:
[1, 3, 2, 3, 1]returns 2;[2, 4, 3, 5, 1]returns 3. Write the recurrence your code satisfies, then differentially test against the double loop on 200 seeded arrays up to length 40, including negatives and values near .After submitting, read the AlgoMaster Reverse Pairs lesson (https://algomaster.io/learn/dsa/reverse-pairs) and compare where the count is taken relative to the merge.
- LeetCode 215: Kth Largest Element in an Array
Resubmit with your deterministic median-of-medians select, with no random pivot. Examples:
[3, 2, 1, 5, 6, 4]with returns 5;[3, 2, 3, 1, 2, 4, 5, 5, 6]with returns 4.The partition unit solved this problem with random pivots; this pass checks the median-of-medians code on the judge's sorted and duplicate-heavy tests.
You're done when
You derive the three recurrences without notes, Reverse Pairs is accepted, and your median-of-medians select matches sorted on every small test and is accepted on LC 215.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 3 more in the section
- DSA Handbook - recursion, backtracking and greedyHandbook Academy · primary
Study Part 7, Part 10 and the relevant foundations on recursion and probability. Write the state definition or greedy claim before opening code.
- Algorithms - recursion, backtracking and greedyJeff Erickson · required
Use the recursion, backtracking, divide-and-conquer and greedy chapters for recurrence derivations, proof templates and counterexamples.
Also in this section
- AlgoMaster DSA - decomposition and greedy sliceAlgoMaster · required
Attempt N-Queens and Gas Station cold, then implement reservoir sampling and test its output distribution over fixed-seed trials. Add another judged problem only for a diagnosed weakness.
- MIT 6.042J - probability and randomized algorithmsMIT OpenCourseWare · support
Use the probability, expectation and concentration readings to derive sampling probabilities and distinguish expected from high-probability guarantees.
- Algorithms lecture notes - randomized algorithmsJeff Erickson · support
Use note 1 Discrete Probability, note 2 Nuts and Bolts (randomized quicksort, with QuickSelect in its exercises), note 4 Tail Inequalities and note 5 Hashing to derive expected costs, separate expected from high-probability guarantees, and analyze hashing behavior.