Worked Examples: Divide and Conquer

These examples are designed to step through the logical process of applying the algorithmic concepts.

Example 1: Merge Sort Recurrence
❓ Problem: Solve $T(n) = 2T(n/2) + n$.
💡 Solution: $O(n \log n)$.
Example 2: Binary Search Recurrence
❓ Problem: Solve $T(n) = T(n/2) + 1$.
💡 Solution: $O(\log n)$.
Example 3: Bad Split
❓ Problem: Solve $T(n) = T(n-1) + n$ (e.g. Selection Sort).
💡 Solution: $O(n^2)$.
Example 4: Ternary Search
❓ Problem: Split into 3 parts, look in 1. $T(n) = T(n/3) + 1$.
💡 Solution: $O(\log_3 n) = O(\log n)$.
Example 5: Max Element D&C
❓ Problem: Find max of array. $T(n)=2T(n/2)+1$.
💡 Solution: $O(n)$. No improvement over iterative.
Example 6: Stooge Sort
❓ Problem: Weird sort. $T(n) = 3T(2n/3) + 1$.
💡 Solution: Apply Master Theorem (Case 1).