Dynamic Array Resizing

Context: FIT1008_MOC · how a fixed Array (Data Structure) backs an unbounded ArrayList · the doubling-amortisation argument

Quick Revision

  • 🎯 Objective: let an ArrayList exceed capacity ➔ allocate a bigger array, copy across, replace, insert.
  • 📦 Core Components: grow by a constant factorappend is amortised (so no is_full).
  • ⚡ Key Constraint: a single append can be (resize copy), but factor growth makes the sequence each; additive growth fails ().

📝 Core

1. The Resize (Allocate → Copy → Replace → Insert)

  • On overflow ➔ allocate a bigger array ➔ copy all elements ➔ replace (old → garbage) ➔ insert.
  • Factor growth ➔ constant-factor resize makes append amortised ➔ why the List (ADT) has no is_full.

2. The Growth Factor (Space–Time Dial)

  • Doubling (×2) ➔ few resizes, up to 2× slack, each new block exceeds sum of all previous (no allocator reuse).
  • ×1.5 (CPython ~1.125+const) ➔ less slack, freed blocks reusable, more copies.
  • Shrinking ➔ needs hysteresis (halve only at ¼ full) to avoid thrashing at a boundary.

⚙️ Core Implementation

🔹 Grow-on-overflow append

⚖️ Core Decision Matrix

SituationappendWhy
room availablewrite + increment
full (resize)allocate + copy
amortisedfactor growth spreads rare copies

When It Flips: three proofs of amortised (doubling) — Aggregate: copies cost each. Accounting: charge 3 credits/append (1 write + 2 banked); never negative. Potential: ; normal append amortised , resize's copy cancelled by the drop in . Same argument powers Hash Table rehashing; amortised ≠ average — a worst-case-sequence guarantee with no probability.

📊 Exam Execution Trace

Manual Execution Trace

Appends into a doubling array (start cap 1):

Step / StateAppend #CapacityResize?Copy cost
0 (Init)10
121→2yes1
232→4yes2
354→8yes4
498→16yes8

Applied Exercise

Problem: Prove the aggregate amortised bound. Derivation Proof / Hand-Calculation Walkthrough:

Final Extracted Output: rare geometric resizes ⟹ amortised append; additive growth would give per append.

🧠 Active Recall