← Course home

Foundations · Lesson 6 of 52

Dynamic-array growth

Relate geometric capacity growth to total copy work and amortized append cost.

Full lessonAbout 9 min

You will learn to explain geometric capacity growth and distinguish amortized append from one resize.

Recommended first: Arrays and Python sequence costs.

On this page 6 sections

The idea

Three appends from capacity 1 copy 0, then 1, then 2 references. The total is 3.

A dynamic array stores items in a contiguous block and remembers a length and a capacity. append writes into the next free slot when length < capacity. When the block is full, the array allocates a new block with twice the capacity, copies the old references, and then writes the new item. The example starts at capacity 1. Appending a, b, and c copies 0, then 1, then 2 references. The library's DynamicArray.capacity after those three appends is 4, and the stored items are a, b, c.

A worked trace

StepStateWhat changed
appendappend = 1Appended a. Length is 1.
resizeresize = 2The grow copied 1 references. Capacity is 2.
appendappend = 2Appended b. Length is 2.
resizeresize = 4The grow copied 2 references. Capacity is 4.
appendappend = 3Appended c. Length is 3.

Why it is correct

Each resize copies every item that is already stored, because the new block is a different region of memory. Doubling is what makes the total copy count linear. The copy sizes are 1 + 2 + 4 + ... + n/2, which is less than n. Every item is copied once per doubling after it arrives. An item that arrives near length n/2 is copied only once more before the length reaches n. Charging each append a constant amount covers all of those copies. That charge is the amortized bound.

A common mistake

Adding one slot each time, instead of doubling, copies a quadratic number of references.

What it costs

One append is O(1) amortized and O(n) in the worst single call, the call that resizes. n appends copy fewer than 2n references in total, so the aggregate time is Θ(n) and the extra memory is Θ(n) for the block. A growth factor of 1, adding one slot each time, would copy Θ(n²) references. The factor, not the mere fact of resizing, is the reason append is cheap on average.

Run it

python
from bitsandbytes.linear.dynamic_array import DynamicArrayarray = DynamicArray()print(array.total_copy_cost_for_appends(3))# prints: 3 

Practice and next step

Open Recursion and recurrences next. Recursion and recurrences turn the same counting habit onto a tree of calls.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.linear.dynamic_array

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Append a, b, and c

Capacity starts at 1 and doubles when an append finds the array full. Copied slots are marked chosen.

append
1

State

  1. 0acomparing
  2. 11comparing

Appended a. Length is 1.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1Capacity starts at 1 and doubles. After 3 appends, how many copies happened?
2Capacity starts at 1 and doubles. After 4 appends, how many copies happened?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.