Chapter 16 · Randomized, Amortized, Streaming

Amortized Analysis: the Doubling Array

A single append can copy the whole array, yet n appends cost less than 3n in total. See the spikes, the running average that stays flat, the coins that prepay the copies, and why growing by "+k" instead of "×2" breaks everything.

View:

Compare growth policies on a big run

Cost = element writes + element copies, starting from capacity 1. "Wasted" = the largest fraction of allocated slots that sat empty.

How to read this

Try this

  1. Doubling, n = 9: grows happen at appends 2, 3, 5 and 9, copying 1, 2, 4 and 8 elements. Total = 9 writes + 15 copies = 24 ≤ 27 = 3·9.
  2. Slide n to 17: append 17 costs 17 by itself, yet the running average stays under 3. Amortized ≠ "every operation is cheap".
  3. Accounting view with charge 3: the balance never goes negative for doubling. Now set the charge to 2: find the first append where the pool goes into debt.
  4. Switch to × 1.5 with charge 3: it goes into debt. What charge keeps it solvent? Theory says 1 + r/(r − 1) = 4 for r = 1.5.
  5. Switch to + k with k = 4 and n = 64. The spikes keep coming every 4 appends and grow taller; no fixed charge survives. In the comparison, raise n and watch "+k" cost per append grow while × 2 stays below 3.

Go deeper