Why Divide and Conquer and What It Gives You in DSA C - Performance Analysis
Divide and conquer breaks a big problem into smaller parts to solve faster.
We want to see how this approach changes the work needed as input grows.
Analyze the time complexity of this divide and conquer example.
void divideAndConquer(int n) {
if (n <= 1) return;
divideAndConquer(n / 2);
divideAndConquer(n / 2);
// constant work here
}
This code splits the problem into two halves, solves each, then combines results.
Look at what repeats in this code.
- Primary operation: Two recursive calls each on half the input size.
- How many times: Calls keep splitting until size is 1, doubling calls each level.
Each step splits the problem, so work grows by doubling calls but input halves.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 calls |
| 100 | About 200 calls |
| 1000 | About 2000 calls |
Pattern observation: Work grows linearly with input size.
Time Complexity: O(n)
This means the total work grows roughly in direct proportion to input size.
[X] Wrong: "Because there are two recursive calls, time doubles each step, so complexity is exponential."
[OK] Correct: Each call works on half the input, so total work adds up linearly, not exponentially.
Understanding divide and conquer time helps you explain why many fast algorithms work well.
"What if the code made three recursive calls instead of two, each on a third of the input? How would time complexity change?"