With a loop, you can count how many times the body runs. A recursive function is harder, because its cost depends on more calls of itself. The trick is to write the cost as a recurrence relation, an equation that describes the work in terms of smaller inputs, and then solve it. This lesson shows three ways to solve one, and checks each answer by counting real calls.
In this lesson you will learn
- How to write a recurrence relation for recursive code
- How to solve it by substitution and with a recursion tree
- How to use the Master Theorem for divide-and-conquer code
- Why some recursive functions blow up to exponential time
Why Recursion Needs Its Own Analysis
Every recursive function does two kinds of work: the work inside one call, and the work of the calls it makes. If T(n) is the total time for input size n, we can write that as an equation. Here are three you will see again and again:
| Code pattern | Recurrence | Result |
|---|---|---|
| One call on n − 1 (factorial) | T(n) = T(n − 1) + c | O(n) |
| One call on half (binary search) | T(n) = T(n/2) + c | O(log n) |
| Two calls on half, plus linear work (merge sort) | T(n) = 2T(n/2) + n | O(n log n) |
| Two calls on n − 1 and n − 2 (naive Fibonacci) | T(n) = T(n − 1) + T(n − 2) + c | O(2n) |
Here c stands for a fixed amount of work, such as one comparison and one multiply. The base case, like n <= 1, is where the recursion stops.
Three Ways to Solve a Recurrence
1. Substitution (Unrolling)
Replace T with its own definition again and again until you see the pattern. For factorial:
T(n) = T(n-1) + c
= T(n-2) + 2c
= T(n-3) + 3c
...
= T(1) + (n-1)c which grows like n, so O(n)
2. The Recursion Tree
Draw each call as a node and write its own work beside it. Then add up the work level by level. For merge sort, the top call does n work. Its two children each do n/2, which is n again for the level. Every level adds up to n, and halving reaches size 1 after about log2 n levels. Total: n × log n.
For naive Fibonacci, the tree almost doubles at every level and is about n levels deep. That gives roughly 2n calls. (The tighter bound is about 1.618n, but it is still exponential.)
3. The Master Theorem
For recurrences of the form T(n) = a·T(n/b) + f(n), compare f(n) with nlogb a:
- If
f(n)grows slower, the leaves win: T(n) = O(nlogb a). - If they grow at the same rate, every level costs the same: T(n) = O(nlogb a log n).
- If
f(n)grows faster (and meets a regularity condition), the top call wins: T(n) = O(f(n)).
Merge sort has a = 2, b = 2, so nlog2 2 = n, the same as f(n) = n. That is the middle case: O(n log n). Binary search has a = 1, b = 2, so n0 = 1, the same as f(n) = c: O(log n).
The theorem does not cover recurrences like Fibonacci’s, where the input shrinks by subtraction rather than division.
Analysing Recursive C Functions
Theory is easier to trust when you can see it. These programs count calls or work and print them for growing inputs.
Factorial vs naive Fibonacci:
#include <stdio.h>
static long calls;
long fact(int n) { /* T(n) = T(n-1) + c -> O(n) */
calls++;
return n <= 1 ? 1 : n * fact(n - 1);
}
long fib(int n) { /* T(n) = T(n-1) + T(n-2) + c -> O(2^n) */
calls++;
return n < 2 ? n : fib(n - 1) + fib(n - 2);
}
int main(void) {
for (int n = 10; n <= 30; n += 10) {
calls = 0; fact(n);
long fc = calls;
calls = 0; fib(n);
printf("n = %2d fact calls: %3ld fib calls: %8ld\n", n, fc, calls);
}
return 0;
}
Output
n = 10 fact calls: 10 fib calls: 177
n = 20 fact calls: 20 fib calls: 21891
n = 30 fact calls: 30 fib calls: 2692537
Factorial calls grow in step with n. Fibonacci calls multiply by about 120 each time n grows by 10, which is what exponential growth looks like.
Recursive binary search:
#include <stdio.h>
static long steps;
/* Binary search: T(n) = T(n/2) + c -> O(log n) */
int bsearch_rec(const int *a, int lo, int hi, int key) {
steps++;
if (lo > hi) return -1;
int mid = lo + (hi - lo) / 2;
if (a[mid] == key) return mid;
return a[mid] < key ? bsearch_rec(a, mid + 1, hi, key)
: bsearch_rec(a, lo, mid - 1, key);
}
int main(void) {
static int a[1 << 20];
for (int i = 0; i < (1 << 20); i++) a[i] = 2 * i;
for (int n = 1 << 10; n <= (1 << 20); n <<= 5) {
steps = 0;
bsearch_rec(a, 0, n - 1, -1); /* key not present: worst case */
printf("n = %8d calls = %ld\n", n, steps);
}
return 0;
}
Output
n = 1024 calls = 11
n = 32768 calls = 16
n = 1048576 calls = 21
The input grew 1,024 times, but the calls only went from 11 to 21. Each extra doubling of n costs one more call: that is O(log n).
Merge sort:
#include <stdio.h>
#include <string.h>
static long work; /* element copies done by merge */
/* Merge sort: T(n) = 2T(n/2) + n -> O(n log n) */
void merge_sort(int *a, int *tmp, int n) {
if (n < 2) return;
int h = n / 2;
merge_sort(a, tmp, h);
merge_sort(a + h, tmp, n - h);
int i = 0, j = h, k = 0;
while (i < h && j < n) tmp[k++] = a[i] <= a[j] ? a[i++] : a[j++];
while (i < h) tmp[k++] = a[i++];
while (j < n) tmp[k++] = a[j++];
memcpy(a, tmp, n * sizeof *a);
work += n;
}
int main(void) {
static int a[1 << 16], tmp[1 << 16];
for (int n = 1 << 10; n <= (1 << 16); n <<= 3) {
for (int i = 0; i < n; i++) a[i] = n - i;
work = 0;
merge_sort(a, tmp, n);
int lg = 0; for (int m = n; m > 1; m >>= 1) lg++;
printf("n = %6d work = %8ld n*log2(n) = %8d\n", n, work, n * lg);
}
return 0;
}
Output
n = 1024 work = 10240 n*log2(n) = 10240
n = 8192 work = 106496 n*log2(n) = 106496
n = 65536 work = 1048576 n*log2(n) = 1048576
The work counted by the merge step matches n × log2 n exactly for these powers of two, just as the recursion tree predicted.
Branching Recursion and Hidden Costs
- Counting levels, not calls. Fibonacci is only
nlevels deep, but the number of calls is exponential. Always ask how many calls each call makes. - Repeated work. Naive Fibonacci works out
fib(n-2)many times. Storing answers (memoisation) cuts it to O(n). - Hidden work inside a call. Copying an array or building a substring in every call adds to
f(n)and can change the answer. - Forgetting space. Each active call uses stack memory. Factorial needs O(n) stack space; very deep recursion can overflow it. See space complexity.
Practice Exercises
Exercise 1. What is the time complexity of T(n) = T(n/2) + n?
Show answer
O(n). The work is n + n/2 + n/4 + …, which adds up to less than 2n.
Exercise 2. Use the Master Theorem on T(n) = 4T(n/2) + n.
Show answer
a = 4, b = 2, so nlog2 4 = n2. Since f(n) = n grows slower, the answer is O(n2).
Exercise 3. A function calls itself three times on n - 1 and does constant work. What is its complexity?
Show answer
T(n) = 3T(n − 1) + c, which is O(3n).
Frequently Asked Questions
What is a recurrence relation?
An equation that gives the running time for input size n in terms of the running time for smaller inputs, plus the work done in one call.
When can I use the Master Theorem?
When the recurrence has the form T(n) = aT(n/b) + f(n), with a at least 1 and b greater than 1. It does not apply when the input shrinks by subtraction, as with T(n − 1).
Is recursion slower than a loop?
The Big O is often the same. Recursion adds a little overhead per call and uses stack space, so a loop can be faster in practice for simple cases like factorial.
How do I make naive Fibonacci faster?
Store each answer the first time you compute it (memoisation), or build the values from the bottom up in a loop. Both bring it down to O(n).
Summary
- Write the cost of recursive code as a recurrence: work in one call plus the cost of its calls.
- Solve it by unrolling, with a recursion tree, or with the Master Theorem.
- One call on n − 1 gives O(n); one call on n/2 gives O(log n); two calls on n/2 plus linear work gives O(n log n).
- Several calls on n − 1 lead to exponential time; memoisation often fixes it.
