FundamentalsFundamentals
Dynamic Array Growth
An array that grows automatically by doubling its capacity, giving amortized O(1) append with O(1) indexed access.
·
0
1/38An empty dynamic array. `length` is how many elements exist; `capacity` is how many the allocated buffer can hold. The gap between them is the whole subject.
Element just appendedBeing copied to the new bufferUnused capacityAlready stored
PseudocodeLearn Dynamic Array →
1append(x):2 if length == capacity: # buffer is full3 newCap = capacity * 2 # geometric growth4 allocate a new buffer of newCap5 copy all `length` elements across <- the O(n) step6 free the old buffer7 buffer[length] = x8 length += 1Variables
length0
capacity1
load0%
reallocs0
totalCopies0
amortized—
Complexity
access O(1)
search O(n)
insert O(n)
delete O(n)
Speed