3.2.1 · D3Linear Data Structures

Worked examples — Array — static, dynamic; cache locality; amortized O(1) append

3,410 words16 min readBack to topic

This page is the stress-test for the parent Array note. There we derived the three big ideas (address formula, geometric growth, cache locality). Here we use them on every kind of input the topic can throw at you — small caps, big caps, a growth factor that isn't 2, an empty array, a resize that happens on the very first append, a real-world timing question, and an exam trap.

Read each [!example] statement, make a forecast before scrolling, then check your instinct against the steps.


The scenario matrix

First, the one symbol the whole page turns on, defined before the table uses it:

Now every problem about dynamic arrays lands in one of these cells. We will hit all of them.

# Case class What makes it special Covered by
A Cheap append (size < capacity) just a write, no copy Ex 1
B Expensive append (size == capacity) triggers a resize + copy Ex 1, Ex 2
C Doubling growth (growth factor ) the classic amortized-O(1) case Ex 2
D Growth factor (e.g. ) still O(1) amortized — must re-derive Ex 3
E Degenerate: empty array / first append capacity 0 → what does "double 0" mean? Ex 4
F Additive growth (+k) — the trap O(n²) total, O(n) amortized Ex 5
G Address arithmetic (static-array flavour) pure B + i·s, sign/edge indices Ex 6
H Real-world timing (cache locality) array vs linked list wall-clock Ex 7
I Exam twist (which grows slower: total copies) compare two growth factors head-to-head Ex 8

Example 1 — Cheap vs expensive appends (cells A, B)

Figure — Array — static, dynamic; cache locality; amortized O(1) append

Figure description (Ex 1). A bar chart with one bar per append: append 10, append 20, append 30, append 40. Each bar has an orange base of height 1 = the single write every append does. The third bar (append 30) additionally carries a violet stack of height 2 = the two elements copied during its resize, so it stands three units tall while all others stay one unit tall and are labelled "cheap." A magenta arrow points at that tall bar reading "full: size==cap triggers resize." The single tall violet bar is the one expensive append; every other bar is a flat cheap write — that shape is the meaning of "amortized": rare spikes averaged across many flat writes.


Example 2 — Total copy cost with doubling (cell C)


Example 3 — Growth factor ≠ 2 (cell D)


Example 4 — The degenerate empty array (cell E)


Example 5 — The additive-growth trap (cell F)


Example 6 — Address arithmetic, edge indices (cell G)


Example 7 — Real-world timing: array vs linked list (cell H)


Example 8 — Exam twist: which factor copies less over appends? (cell I)

Recall Which cells did we cover?

A,B (Ex1) ::: cheap + expensive appends C (Ex2) ::: doubling total copies = 15 for n=16 D (Ex3) ::: g=1.5 still O(1), constant 3 E (Ex4) ::: empty array seeds cap 0→1 F (Ex5) ::: additive +2 gives O(n²) total (12 copies for n=8) G (Ex6) ::: addr(99)=4792, addr(100) is out-of-bounds H (Ex7) ::: array 62,500 fetches vs list ~1,000,000 (16× fewer) I (Ex8) ::: g=4 constant 4/3 < g=2 constant 2


Connections

This page leans on Amortized Analysis and Geometric Series for the copy-cost sums, CPU Cache and Memory Hierarchy for Ex 7, and contrasts arrays with Linked List. The append pattern underlies Stack (Data Structure) (push = append) and the buckets of a Hash Table. Timing claims are stated in Big-O Notation, which is the language for saying "both are yet one is 16× faster in practice."