Sub topic 6
Why growth must be geometric
This chapter assumes you know Big-O notation, how a dynamic array doubles its capacity when full, and the aggregate method from the previous chapters. You should also be able to read short Python programs.
A dynamic array has to choose how much bigger to make the new array when the old one fills up. The earlier chapters used doubling without arguing for it. Here you will see that the choice is not a matter of taste. Growing by a fixed number of slots makes appends cost O(n) amortised. Growing by a constant factor makes them cost O(1) amortised.
The cost model
This chapter uses the same cost model as the aggregate-method chapter:
- Writing one element into the array costs 1.
- Copying one element during a resize costs 1.
- Allocating the new array is free. It is cheap compared with the copying.
An append that finds free space costs 1. An append that finds the array full pays for a resize first. That means it copies every existing element and then writes the new one.
Additive growth: add k slots each time
Suppose the array gains a fixed number of slots k each time it fills. That might be 16 or 1024. The constant does not change the conclusion.
A resize happens roughly every k appends. The j-th resize happens when the array holds about j·k elements, so it copies about j·k elements. After n appends there have been about m = n/k resizes. The total copying is:
k + 2k + 3k + ... + mk = k · m(m + 1) / 2 ≈ n² / (2k)
Divide by n appends to get the average cost per append:
n / (2k) + 1
For a fixed k, this grows linearly with n. The amortised cost is Θ(n), not O(1). A larger k lowers the constant by a factor of k, but it does not change the growth rate. With k = 16 and n = 100 000, each append costs about 3 000 units on average.
Geometric growth: multiply capacity by r
Now suppose the capacity is multiplied by a constant r > 1 on each resize. For doubling, r = 2. The resizes happen when the size equals the capacity. That is at sizes of roughly 1, r, r², r³, and so on, and each size is below n.
The copying cost of a resize equals the size at that moment. The largest such size is below n. The next largest is about n/r, then n/r², and so on. The total copying is bounded by a geometric series:
copies ≤ n · (1 + 1/r + 1/r² + ...) = n · r / (r − 1)
Add the n writes to get the total cost of n appends:
total ≤ n + n · r/(r − 1) = n · (2r − 1) / (r − 1)
For r = 2 this gives 3n, so each append costs at most 3 units amortised. The bound is a constant, so appends are O(1) amortised.
The key difference is the shape of the sum. Additive growth adds up an arithmetic series of copy sizes, which grows like n². Geometric growth adds up copy sizes that shrink by a constant factor as you look further back. Those sizes sum to a constant multiple of the largest one.
A general rule
The two cases fit one principle. Suppose a resize at capacity c adds g(c) new slots. The resize copies about c elements. It then takes about g(c) appends before the next resize. So the copying cost per append is roughly:
c / g(c)
This is O(1) exactly when g(c) is at least a constant fraction of c. That is geometric growth.
Growth that is faster than constant but slower than proportional still fails. For example, adding about √c slots per resize gives roughly √c copies per append. That is an amortised cost of Θ(√n). Only growth proportional to the current capacity gives a constant.
Measuring it
The program below uses the cost model above to compare strategies. It starts with capacity 1. The factor helper forces the capacity to grow by at least one slot, which stops small capacities from rounding to no growth.
from math import isqrt
def total_cost(n, grow):
"""Total cost of n appends: 1 per write plus 1 per element copied on resize."""
size, capacity, cost = 0, 1, 0
for _ in range(n):
if size == capacity:
cost += size # copy every element into the new array
capacity = grow(capacity)
cost += 1 # write the new element
size += 1
return cost
def factor(r):
return lambda c: max(c + 1, int(c * r))
def additive(k):
return lambda c: c + k
strategies = {
"x2": factor(2),
"x1.5": factor(1.5),
"x1.25": factor(1.25),
"+16": additive(16),
"+1024": additive(1024),
"+sqrt(c)": lambda c: c + isqrt(c) + 1,
}
sizes = (1_000, 10_000, 100_000)
print("strategy " + "".join(f"{n:>12}" for n in sizes))
for name, grow in strategies.items():
row = "".join(f"{total_cost(n, grow) / n:12.2f}" for n in sizes)
print(f"{name:<10}{row}")
for n in sizes:
assert total_cost(n, factor(2)) <= 3 * n
assert total_cost(100_000, additive(16)) > 100 * 100_000 / 4Each cell is the average cost per append. The geometric rows stay flat as n grows. The +16 and +1024 rows grow in proportion to n, and the +1024 row only starts climbing once n is well above 1024. The +sqrt(c) row grows too, but more slowly than the additive rows.
The diagram plots the same contrast for doubling and for adding 16 slots.
Choosing the factor
Any r > 1 gives O(1) amortised appends. The constant still matters, and it moves in two directions as r changes.
| Factor r | Bound on amortised cost per append, (2r − 1)/(r − 1) | Worst-case wasted capacity |
|---|---|---|
| 1.25 | 6 | up to 20% of the array |
| 1.5 | 4 | up to 33% |
| 2 | 3 | up to 50% |
| 4 | about 2.33 | up to 75% |
A small r means more frequent resizes, so more copying. A large r means fewer resizes but more unused capacity. As r approaches 1, the bound grows like 1/(r − 1), so a factor of 1.01 is still geometric but costs about 100 units per append. Growing by 1% is therefore constant-time in theory and poor in practice.
Real libraries sit in the middle. Java's ArrayList grows by about 1.5. Common C++ standard libraries use 1.5 or 2, and the standard does not fix the value. CPython lists grow by about 12.5% plus a small constant, and they accept a higher copy cost in exchange for less wasted memory. Go slices double while small and then grow more slowly.
A second factor is peak memory. While a resize runs, the old and new arrays both exist. The peak is (1 + r) times the current size. With r = 2 that is three times the data, which can matter for very large arrays.
Some writers argue that r below the golden ratio (about 1.618) lets an allocator reuse freed blocks for later arrays. That argument depends on the allocator and does not apply to every system. Treat it as folklore and measure before relying on it.
Edge cases and gotchas
- The capacity must strictly increase. With floating-point or integer factors,
int(c * 1.25)is 1 when c = 1. Without a minimum increment of 1, the array never grows. Themax(c + 1, ...)guard in the code handles this. - A factor does not mean the cost is smooth. Every bound here is about the total over a sequence. A single append that triggers a resize still costs Θ(n). That matters in latency-sensitive code, which a later chapter covers.
- Knowing n in advance beats any growth rule. If you can predict the final size, reserve that capacity once. This is what
reservein C++ and theArrayList(int capacity)constructor do. It removes all resize copying. - Growth rules apply to hash tables too. A hash table that grew its bucket count by a fixed amount would have the same problem. Later chapters show that resizing by a constant factor is what keeps insertion O(1) amortised.
Check your understanding
Exercise
A dynamic array grows by a factor of r = 3. Using the bound n · (2r − 1)/(r − 1), what is the largest amortised cost per append under this chapter's cost model, and why is a larger factor not always better?
Show answer
The bound is (2·3 − 1)/(3 − 1) = 5/2 = 2.5 units per append. A larger factor lowers the copying cost, but it leaves more unused capacity (up to 2/3 of the array for r = 3) and raises the peak memory during a resize to (1 + r) = 4 times the current size.
Exercise
An array grows by 1% of its capacity on each resize, with a minimum increment of 1. Is appending O(1) amortised? Is this a good design?
Show answer
Yes, it is O(1) amortised, because the growth is proportional to the capacity (r = 1.01). The bound is (2·1.01 − 1)/(0.01) = 102 units per append, so the constant is large. It is a poor design for typical use. A factor like 1.5 or 2 cuts the copying cost by a large factor at a modest cost in memory.
Exercise
Why does growing the capacity by about √c slots on each resize give an amortised cost of Θ(√n) per append?
Show answer
A resize at capacity c copies about c elements. The next resize comes after about √c more appends. So the copying cost per append is about c/√c = √c. Since c is close to n, the amortised cost is Θ(√n). That is better than a fixed increment but still not constant, because the increment is not proportional to c.