Dynamic Array Growth and Capacity Hysteresis

Count copies under geometric resizing and choose a shrink threshold that prevents repeated resizing around one boundary.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Bound all the copies in a growing array
  2. Separate shrinking from growing

Bound all the copies in a growing array

Begin with capacity one and double it whenever an append would exceed capacity. Before reaching capacity \(2^k\), growth has copied \(1+2+\cdots+2^{k-1}=2^k-1\) elements. The final capacity is less than twice the final size whenever growth occurred. Total work over \(n\) appends is therefore \(O(n)\), with one append possibly doing linear work. The same proof works for fixed growth factors above one, with a larger copy constant for factors nearer one. This is an aggregate amortized bound.

Separate shrinking from growing

If an array shrinks as soon as it becomes half full, alternating insertion and deletion near a boundary can trigger repeated reallocations. Shrinking to half capacity at one-quarter occupancy leaves the new array roughly half full. Many appends or deletions must then occur before another resize. For a long-lived service, amortized versus per-operation latency explains when copying must be spread across operations rather than paid all at once.[1][2]

References

  1. MIT OpenCourseWare, Introduction to Algorithms: Lecture Notes ↩
  2. MIT OpenCourseWare, Design and Analysis of Algorithms: Lecture Notes ↩