Dynamic arrays: capacity, growth, amortized append
A dynamic array grows by copying into a bigger block when it fills. Doubling the capacity keeps appends O(1) on average.
Before you start: Arrays: indexing, traversal, insertion and deletion costs.
The idea
A plain array has a fixed size. Once all 4 cells are used, there is no cell 4 to write into.
Python lists, Java ArrayLists and C++ vectors still let you append forever. Underneath they are plain arrays that move to a bigger block when full.
Intuition
When the block is full, take a block twice as big, copy every value over, then free the old one.
After a resize, the next appends land in free slots. Each one is a single write with no copying.
Capacity is how many cells the block has. Size is how many hold values. Only an append that finds size = capacity pays for a copy.
Complexity
With doubling, n appends copy fewer than 2n values in total. Growing by 1 copies about n² / 2.
Resizes copy 1 + 2 + 4 + … values, a sum below 2n. Spread over n appends, each append costs O(1) amortized: a constant on average, although one append can cost O(n).
Space stays O(n): right after a resize at most half the block is unused.
Pitfalls
Growing by one cell makes every append copy the whole array. n appends then cost O(n²).
Adding a constant, say 10 cells, only divides the copying by 10. It is still O(n²). Only multiplying the capacity keeps appends O(1) amortized.
Shrinking at half full is a trap. A pop shrinks 8 to 4, the next push grows it back, and each one copies.
Shrink only when the array is a quarter full, and halve. After any resize it is half full, so many cheap operations come before the next one.
Variations
Java grows by 1.5× and Python by about 1.125×. Any factor above 1 keeps appends O(1) amortized.
Code
Pseudocode
push(x)
if size = capacity
bigger ← new block of 2 × capacity
for k ← 0 to size - 1
bigger[k] ← data[k]
data ← bigger, capacity ← 2 × capacity
data[size] ← x
size ← size + 1
Python
class DynArray:
def __init__(self, capacity):
self.data = [None] * capacity
self.size = 0
def push(self, x):
if self.size == len(self.data):
bigger = [None] * (2 * len(self.data))
for k in range(self.size):
bigger[k] = self.data[k]
self.data = bigger
self.data[self.size] = x
self.size += 1
JavaScript
class DynArray {
constructor(capacity) {
this.data = new Array(capacity);
this.size = 0;
}
push(x) {
if (this.size === this.data.length) {
const bigger = new Array(2 * this.data.length);
for (let k = 0; k < this.size; k++) bigger[k] = this.data[k];
this.data = bigger;
}
this.data[this.size] = x;
this.size++;
}
}
Java
class DynArray {
int[] data = new int[2];
int size = 0;
void push(int x) {
if (size == data.length) {
int[] bigger = new int[2 * data.length];
for (int k = 0; k < size; k++) bigger[k] = data[k];
data = bigger;
}
data[size] = x;
size++;
}
}
Cost at a glance
| Access | O(1) |
|---|---|
| Search | O(n) |
| Insert | O(1) amortized at end / O(n) |
| Delete | O(1) at end / O(n) |
| Space | O(n) |
Practice problems (6)
Capacity after k appends Warm-up
A dynamic array starts empty with capacity 1 and doubles when full. Append 4, 8, 15, 16, 23, 42 and answer each question before the steps play.
Count the copies of n appends Warm-up
A dynamic array starts empty with capacity 1. Before an append that finds the block full, it doubles the capacity and copies every stored value into the new block.
Return how many values are copied in total during n appends.
Implement push with doubling Standard
data is the backing block of a dynamic array: its length is the capacity, the first size cells hold values and the rest hold 0.
Append x and return the backing block afterwards. If the block is full, first make a new block of twice the length (length 1 when it was empty), copy the size values into it with a loop, and fill the remaining cells with 0. Do not use built-in growth such as push, append or concat.
Why 2× and not +1 Standard
Two dynamic arrays start at capacity 1 and receive the same 1,000 appends. Array A doubles its capacity when full. Array B adds one cell when full. Which statement is true?
Cost of building a list by appending Standard
build(n) appends n values to an empty dynamic array that doubles when full. Pick its total time and its space.
Implement a dynamic array with pop and shrink Challenge
Build a dynamic array on a plain backing block and run a list of operations. It starts empty with capacity 1.
"push x"appends the integer x. If the block is full, first copy the values into a block of twice the capacity."pop"removes the last value. Afterwards, if the capacity is above 1 and size ≤ capacity / 4, copy the values into a block of half the capacity.
Return the capacity after each operation.
Next on the path: Singly linked lists: traverse, insert, delete