How Dynamic Arrays Grow Efficiently
A dynamic array stores an ordered sequence in an indexed block of storage that can be replaced when more room is needed. You encounter this implementation when a program collects an unpredictable number of values while retaining fast access by index. Its central trade-off is simple: reserve some unused space so that most appends avoid allocating and copying another block.
Size, capacity, and valid indices
Track two quantities: the size n and the capacity C. Their governing relationship is
With zero-based indexing, existing elements occupy indices 0 through n−1. Unused slots from n onward do not belong to the logical sequence, even though storage has been allocated for them. An indexed read therefore requires 0≤i<n.
For an append, the next available position is index n. If n<C, write the new element there and increase n by one. Capacity stays unchanged.
Growing without losing order
When the block is full, an append needs more storage. Under a doubling policy, allocate a block with capacity 2C, copy the C existing elements to the same indices, replace the reference to the old block, and then append the new element. Assume the initial capacity is positive.
Resize before writing beyond the allocated block. Copying preserves both values and order; it does not add elements to the logical sequence. Only the append increases size.
The stored elements may be fixed-size values or references to objects. Copying a reference does not require copying the entire object. The cost analysis below assumes that moving one stored element takes constant time.
Doubling is an explicit implementation policy, not a universal requirement. Other fixed growth factors greater than one also support efficient repeated appends. Increasing capacity by a fixed number of slots requires copying an increasingly large block after every fixed number of appends, producing quadratic total copying work.
Why repeated appends are efficient
An append into spare capacity takes constant time. An append that triggers growth takes Θ(n) time because it copies all n existing elements.
The amortized cost is the cost per operation obtained by bounding the total work of a sequence and spreading that bound across its operations. It does not require random inputs, and it does not claim that every individual append is cheap.
Starting empty with capacity one, count existing-element copies and writes of newly appended values. Ignore allocation and bookkeeping in this write-count model. For m appends, the copied block sizes form a geometric sequence:
Only blocks that actually become full and are replaced contribute to the sum. Consequently, the amortized append cost is Θ(1).
Example: Ten appends from an empty block of capacity one copy 1+2+4+8=15 existing elements. Including ten new-element writes gives 25 writes overall, even though the append that replaces the eight-slot block performs nine writes by itself.
Costs to keep separate
Spare capacity helps append operations, but it does not eliminate movement needed to preserve order during insertion or removal. Here, i is a valid operation index, and the last two rows assume no resizing.
Operation | Time | Reason |
|---|---|---|
Read or replace an existing element | Θ(1) | Access one indexed slot |
Append with spare capacity | Θ(1) | Write one new element |
Append requiring growth | Θ(n) | Copy existing elements first |
Insert at index i | Θ(n−i+1) | Move the suffix and write the new element |
Remove at index i | Θ(n−i) | Move the following elements left |
Shrinking is also a policy choice. Shrinking at half capacity can cause alternating removals and appends to repeatedly shrink and grow the block. A lower threshold, such as one-quarter full when growth doubles capacity, leaves room between the two triggers.
What to remember
- Size counts logical elements; capacity counts allocated slots.
- Resizing preserves existing indices and order.
- Geometric growth gives constant amortized append time, with occasional linear-time appends.
- Inserting or removing elements can still require shifting a suffix.
Example: Growing a Sensor-Reading Buffer
A monitoring program stores six sensor readings in a dynamic array with capacity eight. Indices start at zero. The program appends five more readings, doubling capacity immediately before any append that encounters a full block. It does not shrink storage.
Count one element write whenever an existing reading is copied or moved, or a newly added reading is stored. Ignore allocation, reads, and size bookkeeping. Each stored reading takes constant time to copy.
a) Find the size and capacity after all five appends.
The initial unused space is ________, because capacity minus size is 8−6=2. The first two appends therefore increase size from six to eight without changing capacity.
Before the next append, the full block contains ________. The third append triggers growth from capacity eight to capacity sixteen. Copying preserves the eight existing readings and their indices; writing the third new reading then increases size to nine.
The fourth and fifth appends use spare slots, increasing size to ten and then eleven. Thus,
The final size is 11 readings, and the final capacity is 16 slots.
b) How many element writes do the five appends require altogether?
There is exactly one resize. It copies ________, one for each reading present when the original block becomes full. These copies do not increase the logical size.
The five appends also require ________ to store their five new readings. Count these separately from copying the old readings:
As a check, the third append performs eight copies plus one new-reading write, while each of the other four appends performs one write. That also gives 9+4=13.
The five appends require 13 element writes altogether.
c) The program now inserts one additional reading at index 4, preserving order. How many element writes does this insertion require?
Before insertion, size is eleven and capacity is sixteen, so there is room for the additional reading. No resize is needed, but order still requires movement.
The readings currently at indices 4 through 10 must move one position right. Work from the end toward index 4 so that a reading is moved before its slot is overwritten. The number moved is ________, calculated as
After these moves, store the new reading at index 4. This requires one additional write:
The insertion requires 8 element writes. Size becomes twelve readings, and capacity remains sixteen slots.
A logging program starts with an empty dynamic array of capacity 4. Whenever an append finds the buffer full, the program doubles its capacity and copies all existing elements. Count each element copy and each new-element write as one unit of work; ignore allocation overhead.
Complete the two analysis names using amortized or worst-case.
Bounding the total work across a sequence of appends and dividing by the number of appends gives the cost per append.
Bounding the work of the most expensive individual append gives the cost per append.
A dynamic array initially has size 3 and capacity 5, meaning five element slots are allocated. It doubles its capacity immediately before an append when the current block is full. No other operation changes capacity. Complete the table after the stated cumulative numbers of appends, entering exact integers for size and capacity.
| Appends completed | Size | Capacity |
| 1 | ||
| 2 | ||
| 3 | ||
| 8 |