5 · Loop-analysis rules & space complexity
Quick rules for nested and halving loops, plus how much memory an algorithm uses.
In one sentence
To get the running time of iterative code, work inside-out: multiply the work inside a loop by how many times the loop runs, add consecutive blocks, and take the bigger branch of an if/else. Space complexity counts the extra memory the algorithm needs as grows.
Everyday analogy
Nested loops = a clock. The minute hand (inner loop) goes all the way round 60 times for every one step of the hour hand (outer loop). Total minute-steps in 12 hours = . When the inner loop's length depends on the outer one, you can't multiply; you add up the rounds instead (a sum).
Space = desk space while doing homework. The textbook is the input (you'd have it anyway). Space complexity asks how much scratch paper you need, and whether it grows with the size of the problem.
The four rules
| Rule | Code shape | Time |
|---|---|---|
| 1. Loop | for times { body } | (time of body) |
| 2. Nested loops | analyse inside-out | product of the loop sizes (if independent) |
| 3. Consecutive statements | block A; block B | time(A) + time(B), so the bigger one wins |
| 4. If/else | if test then S1 else S2 | test + max(S1, S2) for the worst case |
Loop patterns to recognise instantly
| Pattern | Iterations | Growth |
|---|---|---|
for i ← 1 to n | ||
for i ← 1 to n / for j ← 1 to n | ||
for i ← 1 to n / for j ← 1 to i | ||
i ← 1; while i < n: i ← 2i | ||
i ← n; while i > 1: i ← i/2 | ||
for i ← 1 to n / doubling j loop inside | ||
i ← n; while i ≥ 1: (do i work); i ← i/2 | ||
| three nested loops |
Dependent nested loop as a staircase (for i ← 1 to n / for j ← 1 to i), with :
i=1 ■
i=2 ■ ■
i=3 ■ ■ ■
i=4 ■ ■ ■ ■
i=5 ■ ■ ■ ■ ■ 15 squares = 5·6/2 → about half of the 5×5 square → Θ(n²)Doubling loop (i ← 1; while i < n: i ← 2i), with :
| iteration | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| after | 2 | 4 | 8 | 16 | 32 | 64 | 128 ≥ 100, stop |
7 iterations, and ✓
Halving outer loop with inner work (looks like but isn't):
i = n ■■■■■■■■■■■■■■■■ (16)
i = n/2 ■■■■■■■■ (8)
i = n/4 ■■■■ (4)
i = n/8 ■■ (2)
i = 1 ■ (1) total 31 = 2n − 1 → Θ(n)Common mistakes
- Multiplying loop sizes when the inner bound depends on the outer variable. Write the sum instead.
- Seeing two nested loops and automatically saying . The halving-outer pattern above is .
- Forgetting the recursion stack in space analysis.
- Counting the input array itself as extra space. Space complexity usually means auxiliary space; say which one you mean.
Mastery check
Solve without hints, then explain each step
i ← n
while i ≥ 1 do
for j ← 1 to i do
x ← x + 1
i ← ⌊i / 2⌋(a) How many times does x ← x + 1 run when ? (b) Give in general. (c) What is the extra space?
(a) .
(b) , so (a shrinking geometric series).
(c) .
Status
Tick all six steps to unlock “Mastered”.
Which study mode worked for you?
References
- Lecture06 Algorithm Analysis: Slides 3–4 (the four rules), 5–9 (loop examples).
- 04 Correctness & Space: Slides 6–9 (space complexity: fixed vs. variable part, arrayMax).
- 03 Algorithm Analysis: Slides 7–8 (a triple-nested brute-force loop).
- CLRS 4e §2.2 (analysing algorithms), Appendix A (sums used for loops).