← capítulo

Arrays and dynamic arrays

Contiguous memory → O(1) random access. Fixed size → grow by doubling.

The append problem

Amortized O(1)

1+2+4++n=2n1=O(n)1 + 2 + 4 + \cdots + n = 2n - 1 = O(n)

Total copying over n appends is linear → O(n)n=O(1)\frac{O(n)}{n} = O(1) per append.

Trace: 63 copies over 64 appends ≈ 1 each.

Copies per append: the sawtooth

Spikes at 1, 2, 4, 8, 16, 32 — twice as far apart each time.

Watch it fill and double

Costs

Takeaway

Cheap at the end and by index, expensive in the middle. That's the array — and the baseline every later structure reacts to.