4. Adapting D&C to a NEW problem (the LO1 drill — Applied 2)
Three questions, in order ➔ (1) does the answer decompose additively across the split — within-left + within-right +cross? (2) can the cross term be computed in Θ(n)? (3) does the per-call work shrink with the subproblem, or does the level sum refuse to decay?
Strengthen the recursive contract ➔ ask the recursion to return more than the answer. Counting Inversions is only Θ(nlogn) because each call returns a sorted subarray as well as a count; that extra guarantee is what makes the cross term linear.
Question (3) is the one that gets skipped ➔ 2D Local Maximum (Peak Finding) halves the matrix on the middle column and still costs Θ(nlogn), because the deciding scan stays full-length. Cutting both axes makes level i cost cn/2i ⟹ Θ(n).
On a new problem, the correctness argument is the deliverable ➔ “why is it safe to discard the other subproblems?” carries more marks than the pseudocode; state it as an explicit claim about what the kept subproblem is guaranteed to contain.
⚙️ Core Implementation
Split/combine trade-off:Merge Sort = trivial split, heavy combine; Quick Sort = heavy split, trivial combine; Binary Search = single-subproblem “decrease and conquer”.
🔹 The D&C skeleton
generic divide-and-conquer sort
def sort(array) -> None: if len(array) > 1: # base case: len <= 1 already sorted split(array, first_part, second_part) sort(first_part) # conquer each half sort(second_part) combine(first_part, second_part)
💡 Common Mistake:Shrink by factor vs by one ➔ halving gives depth logn, peeling one element gives depth n — the Θ(nlogn) vs Θ(n2) divide and quicksort’s bad-pivot degeneration.
When It Flips: balanced splits give depth logbn; lopsided ones push depth toward n, collapsing Θ(nlogn) to Θ(n2). Space: recursion stack Θ(depth); merge-style combine adds Θ(n) scratch, partition-style is in-place.
📊 Exam Execution Trace
Manual Execution Trace
Recursion tree of a balanced D&C sort on n=8:
Step / State
Level
# subproblems
Size each
Work this level
0 (Init)
0
1
8
Θ(8)
1
1
2
4
Θ(8)
2
2
4
2
Θ(8)
3
3
8
1
Θ(8)
log28+1=4 levels, each Θ(n) ⟹ Θ(nlogn).
Applied Exercise
Problem: Derive the balanced vs lopsided D&C recurrences.
Derivation Proof / Hand-Calculation Walkthrough: