Sub topic 14
Floors, Ceilings, and Base Cases Without Worry
The recurrences in earlier chapters were written as if n/2 were always a whole number and as if the base case never mattered. Real code doesn't behave that way. In the merge sort example, mid = len(xs) // 2 gives the left half ⌊n/2⌋ elements and the right half ⌈n/2⌉ elements. The exact recurrence is:
T(n) = T(⌊n/2⌋) + T(⌈n/2⌉) + n, with T(1) = 1.
This chapter explains why you can usually drop the floors and ceilings and pick any convenient constant base case. It also covers the few situations where that shortcut goes wrong.
This chapter assumes you are comfortable with Big-Theta notation, the substitution method, proof by induction, and the statement of the master theorem.
Why rounding shifts are harmless
A ceiling adds less than 1 to its argument: ⌈n/2⌉ ≤ n/2 + 1. Follow that error down the recursion. After two levels the argument is at most (n/2 + 1)/2 + 1 = n/4 + 1.5. After k levels it is at most n/2^k + 2. The rounding errors form a geometric series, so the total drift stays below a fixed constant at every depth. A subproblem that is "n/2^k plus a constant" has the same size as n/2^k up to a constant, so the work at each level changes by a bounded factor at most.
That is the intuition. The next section gives an argument you can write down as a proof.
The sandwich argument: solve on exact powers, then extend
The usual rigorous route has two steps:
- Solve the recurrence only for n = b^k, where every division is exact. Use any method from earlier chapters.
- Extend the result to all n using monotonicity.
Suppose the following three facts hold:
- T is nondecreasing: m ≤ n implies T(m) ≤ T(n). For the floor and ceiling version of merge sort you can prove this by induction on n.
- T(b^k) = Θ(g(b^k)) for some nondecreasing function g.
- g is smooth: g(bn) = O(g(n)).
Now take any n with b^k ≤ n < b^(k+1). Monotonicity traps T(n) between its neighbouring powers:
T(b^k) ≤ T(n) ≤ T(b^(k+1)).
For the upper bound, T(b^(k+1)) ≤ C·g(b^(k+1)) ≤ C·K·g(b^k) ≤ C·K·g(n). For the lower bound, T(b^k) ≥ c·g(b^k) ≥ (c/K)·g(b^(k+1)) ≥ (c/K)·g(n). Both bounds are constant multiples of g(n), so T(n) = Θ(g(n)) for every n.
Smoothness is the condition that carries the argument. It says that moving to the next power of b changes g by at most a constant factor.
| g(n) | Smooth (g(2n) = O(g(n)))? | Sandwich argument works? |
|---|---|---|
| n^d (log n)^k | Yes, g(2n) ≤ 2^d · 2^k · g(n) for n ≥ 2 | Yes |
| n^(log_b a) | Yes | Yes |
| 2^n | No, g(2n) = g(n)^2 | No |
| n! | No | No |
Every bound the master theorem produces has the form n^d (log n)^k, so it is always smooth. Exponential bounds are not smooth. For those you must keep the exact arguments or use a different technique.
Seeing it numerically
The program below evaluates merge sort's recurrence exactly, with floors and ceilings, for sizes that are not powers of two. It prints the ratio T(n) / (n log₂ n). It runs the recurrence twice: once with base value 1 and once with base value 100.
import math
from functools import lru_cache
@lru_cache(maxsize=None)
def T(n, base):
# Exact merge sort recurrence: floor half, ceiling half, linear merge
if n <= 1:
return base
return T(n // 2, base) + T((n + 1) // 2, base) + n
for n in [1_000, 10_007, 123_457, 1_000_003, 9_999_991]:
r1 = T(n, 1) / (n * math.log2(n))
r100 = T(n, 100) / (n * math.log2(n))
print(f"{n:>10} base=1: {r1:.4f} base=100: {r100:.4f}")Both ratio columns stay bounded and drift toward 1 as n grows. The sizes are deliberately awkward (several are prime), and the floors and ceilings do not disturb the n log n shape. The base=100 column starts higher and closes the gap slowly. The next section explains why.
Base cases: why the constant usually doesn't matter
A divide-and-conquer recursion tree has about n^(logb a) leaves. If each leaf costs T(1) = β instead of 1, the leaf level contributes β·Θ(n^(logb a)), which is still Θ(n^(log_b a)). The internal levels do not depend on β at all. Changing the base value therefore changes the result by at most a constant factor.
The base-case threshold works the same way. Suppose the recursion stops at every n ≤ n₀, as in a merge sort that switches to insertion sort below 16 elements. The tree loses its bottom log_b n₀ levels, and each leaf now costs some fixed amount bounded by a function of n₀. Since n₀ is a constant, the asymptotic class does not change.
In the merge sort program, raising β from 1 to 100 adds about 99n to T(n). That term is O(n), so it fades relative to n log n. This is why the base=100 ratio converges to 1 slowly but still converges.
Gotcha 1: the recursion must actually reach the base case
With floors, the argument always shrinks: ⌊n/2⌋ < n for n ≥ 1. Ceilings and additive shifts are less forgiving. ⌈n/2⌉ = 1 when n = 1, so a base case of "n = 0" alone would never be reached. A recurrence such as T(n) = T(⌈n/2⌉ + 1) + 1 is stuck at n = 2 (since ⌈2/2⌉ + 1 = 2) and at n = 3 (since ⌈3/2⌉ + 1 = 3).
The rule: the base case has to cover every n at which the subproblem size fails to drop strictly below n. The helper below finds those values by brute force.
def stuck_values(step, n0, upto=100_000):
"""Return every n > n0 where step(n) does not strictly decrease."""
return [n for n in range(n0 + 1, upto) if not step(n) < n]
ceil_half = lambda n: (n + 1) // 2 # ceil(n/2) using integers only
ceil_half_plus_1 = lambda n: (n + 1) // 2 + 1
print(stuck_values(ceil_half, 1)) # []
print(stuck_values(ceil_half_plus_1, 1)) # [2, 3]
print(stuck_values(ceil_half_plus_1, 3)) # []For ceil_half_plus_1, the base case must therefore cover n ≤ 3. Above that threshold, the argument is at most n/2 + 2, which keeps shrinking. After a constant shift the recurrence behaves like T(n/2) + 1 and solves to Θ(log n).
Gotcha 2: the induction base in the substitution method
Asymptotic notation only asks that a bound holds for n ≥ n₀. You get to choose n₀, and the substitution method relies on that freedom.
Suppose you try to prove T(n) ≤ c·n log₂ n for merge sort's recurrence. The bound fails at n = 1, because the right side is c·1·0 = 0 while T(1) = 1. No choice of c fixes this. The fix is to start the induction at a larger value. The inductive step for n ≥ 4 only reaches arguments ⌊n/2⌋ and ⌈n/2⌉, which are at least 2. So check n = 2 and n = 3 directly as the induction base, and choose c large enough to cover them: T(2) = 4 and T(3) = 8, so any c ≥ 2 works. The value T(1) still appears in the calculation but never needs to satisfy the bound itself.
Summary of the working rules
- For bounds of the form n^d (log n)^k, solve on exact powers of b. Monotonicity extends the result to every n.
- Any constant base value and any constant cutoff n₀ give the same Θ-class, as long as the recurrence is linear in T.
- Check that every recursive argument is strictly smaller than n above your base threshold. Ceilings and added constants can create values where the recursion gets stuck.
- In substitution proofs, start the induction wherever the bound actually holds. Small cases go into the induction base.
Exercise
Solve T(n) = 3T(⌈n/4⌉) + n² and say which base case it needs.
Show answer
⌈n/4⌉ < n for every n ≥ 2, so a base case at n = 1 is enough. log_4 3 ≈ 0.79 < 2. Regularity holds because 3(n/4)² = (3/16)n². So case 3 gives Θ(n²), and the floor and ceiling version of the master theorem keeps that answer.
Exercise
For T(n) = T(⌊n/2⌋ + 17) + n, find the largest n at which the argument fails to decrease. Then give the asymptotic solution.
Show answer
⌊n/2⌋ + 17 < n first holds for every larger n starting at n = 35. At n = 34 the argument is 17 + 17 = 34, so the recursion is stuck there, and some smaller values (for example 33) are stuck too. The base case must cover n ≤ 34. Above that, the argument is about n/2 plus a constant, so the recurrence behaves like T(n/2) + n, and case 3 gives Θ(n).
Exercise
Someone shows that T(2^k) = Θ(2^(2^k)) and concludes from monotonicity that T(n) = Θ(2^n) for all n. What is wrong?
Show answer
g(n) = 2^n is not smooth. Between consecutive powers of two, g jumps from 2^(2^k) to 2^(2^(k+1)), which is the square of the smaller value, not a constant multiple of it. The sandwich only traps T(n) between two values that are far apart, so it proves neither Θ(2^n) nor any other tight bound.
Exercise
Why can't the substitution proof of T(n) ≤ c·n log₂ n for merge sort use n = 1 as its induction base, and what replaces it?
Show answer
At n = 1 the bound is c·1·0 = 0, but T(1) = 1, so it fails for every c. Use n = 2 and n = 3 as the induction base instead, with c ≥ 2 to cover T(2) = 4 and T(3) = 8. For n ≥ 4 the inductive step only reaches arguments of at least 2, so it never relies on the bound at n = 1.