A dynamic array is a method of enlarging arrays when we need them.

We start with an array of size and double its size from from whenever we run out of space. The process is as follows:

  • Allocate a new continuous array of size
  • Copy contents of old array to the lower half of the new one
  • Return space used by old array to storage allocation system

The waste in this procedure involves copying our contents on each expansion.

  • For some number of slots , it will take doublings until the array has positions, plus one final doubling on the last insertion.
  • There are copying operations on first, second, eight, , th insertions.
  • The number of copy operations at the th doubling will be , so the total number of operations will be:

Thus, each of the elements is only moved twice on average. The total work of managing the dynamic array is the same as it would have been if a single array of sufficient size had been allocated in advance.

By using dynamic arrays, the thing we are losing is the guarantee that each insertion takes constant time in the worst case. All accesses and most insertions will be fast, except for those relatively few insertions that trigger array doubling. What we get instead is an amortized guarantee that the th element insertion will be completed quickly enough that the total effort expended so far will still be .