Dynamic array doubling

ID: dynamic-array-doubling

Consider a dynamic array holding fixed-size entries. Its size is the number of occupied slots and its capacity is the number of allocated slots. Start with , . The only operation is append: if , allocate a new array of capacity , copy the existing entries, and release the old array; then write the new entry and increase by one. There is no reserve operation, deletion or shrinking in this model.
Count one unit for writing the new entry and one unit per existing entry copied during resizing. Thus the th append has actual cost without resizing and when the old capacity is full. Copying an entry, including its read and write, is one counted action. Allocation, deallocation, index arithmetic operations and the initial one-slot allocation are outside this exact count. If these add only a constant per ordinary append and a linear cost per resize, the Big O notation conclusions still hold, with different constants; costly entry copies require their own cost model.
The first ten costs are . For example, append copies eight entries and writes one, so . At that point the capacity becomes , but the total cost of all nine appends is only .
For , the final capacity is . Every earlier capacity was copied once. This geometric series sums to , including the empty sum when . Therefore aggregate analysis gives the exact total
Consequently , so append has amortized cost in Big O notation for every prefix from this empty initial state. The values being appended do not affect the count. In contrast, an append to a full dynamic array with entries costs : its worst-case cost is , and actually grows linearly along full-capacity states. A bound on total work does not promise a constant response time for each call.
Figure 1.
Individual append costs and cumulative work under dynamic array doubling
. The spikes include every copied entry and the new write. At append 33 the actual cost is 33; at append 64 the total cost is 127. Every prefix lies below the line . Details.
The same policy controls computer memory: after each completed append, for , so fewer than slots are unused. This is a bound on the retained backing array, with fixed-size slots. During a resize from full capacity to , a copying implementation can temporarily hold both arrays, totalling allocated slots before releasing the old one. Thus the retained-capacity bound does not bound this temporary peak by twice the live size. Allocator metadata and separately allocated objects are additional computer memory costs.
The geometric series is the source of the efficiency. If capacity instead increases by one each time, the same count becomes , a quadratic total. Dynamic array doubling buys a linear total with bounded spare capacity, even when the final length is unknown.

New to topics? Read the docs here!