← Course home

Foundations · Lesson 7 of 52

Recursion and recurrences

Translate recursive calls into recurrences and count levels, work and stack space.

Full lessonAbout 10 min

You will learn to recognize common recurrence shapes and count recursive levels and stack memory.

Recommended first: Asymptotic growth.

On this page 6 sections

The idea

A merge-divide tree on n = 8 has 4 levels, and each level does 8 units of work.

A recurrence describes a function that calls itself. The one you will meet in merge sort is T(n) = 2 T(n/2) + Θ(n), with T(1) = Θ(1). The 2 T(n/2) is the two recursive calls. The Θ(n) is the work to merge the halves. Unrolling the recurrence shows log2(n) levels, because the input size halves each time. Each level does Θ(n) work, because the pieces at one level add up to n. The product is Θ(n log n). The call stack on one path is Θ(log n) frames deep. That is extra memory even when each frame stores only a few indexes.

A worked trace

StepStateWhat changed
Level 0work = 8Level 0 has 1 calls of size 8. The level still does 8 work.
Level 1work = 8Level 1 has 2 calls of size 4. The level still does 8 work.
Level 2work = 8Level 2 has 4 calls of size 2. The level still does 8 work.
Level 3work = 8Level 3 has 8 calls of size 1. The level still does 8 work.

Why it is correct

The level count is exact for powers of two. n, n/2, n/4, ..., 1 is log2(n) steps. At level i there are 2^i calls and each call works on n / 2^i items, so the level sums to n. Adding log2(n) levels of n work gives n log2(n). The base case stops the recurrence, which is the same role termination plays for a loop. An unbalanced recursion such as T(n) = T(n - 1) + Θ(n) has n levels and Θ(n²) total work. The shape of the calls is the whole story.

A common mistake

Depth and total work are different. A short depth can still do linear work on every level.

What it costs

Time is the unrolled sum, Θ(n log n) for the balanced split. Stack memory is proportional to the deepest chain of calls. It is Θ(log n) when every split is even. It is Θ(n) when each call peels off one item. Report the time and the stack. A time bound does not imply a stack bound.

Merge sort is this recurrence with a balanced split. A quicksort pivot that always peels off one item is the other shape, T(n) = T(n - 1) + Θ(n). Same lesson, different tree. Ask which shape you have before you quote n log n.

Run it

python
from bitsandbytes.sorting.merge_sort import merge_sortprint(merge_sort([3, 1, 4, 2]))# prints: [1, 2, 3, 4] 

Practice and next step

Open Singly linked list next. A singly linked list is the first structure whose cost depends on which pointer you hold.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.complexity

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Halve 8 until 1

Each call splits into two half-size calls. This frame shows one root-to-leaf path and the size at that level.

work
8

State

  1. 01comparing
  2. 18comparing
  3. 28comparing

Level 0 has 1 calls of size 8. The level still does 8 work.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1A recursion splits in half and does linear work. What is the total?
2A call does Θ(n) work and then one call on n - 1. What is the total?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.