1. Arrays and Dynamic Arrays
A block of contiguous memory with O(1) access by index. A dynamic array (Python list, Java ArrayList, C++ vector) doubles its capacity when full, so appending stays O(1) on average.
How it works
An array stores items side by side in memory, so the address of item i is start + i * item_size. That is why reading a[i] is O(1) no matter how big the array is. The cost is at the edges: inserting or deleting in the middle means shifting everything after that position.
A fixed-size array cannot grow. A dynamic array keeps a hidden buffer with spare room (its capacity). When the buffer is full, it allocates a buffer twice as large and copies every element across. Copying is O(n), but it happens so rarely that the total cost of n appends is under 3n element writes, which is amortized O(1) per append.
Diagram
flowchart LR
s0["capacity 1: 10"]
s1["capacity 2: 10 20"]
s2["capacity 4: 10 20 30 _"]
s3["capacity 4: 10 20 30 40"]
s4["capacity 8: 10 20 30 40 50 _ _ _"]
s0 -->|"append 20: full, copy 1"| s1
s1 -->|"append 30: full, copy 2"| s2
s2 -->|"append 40: room left"| s3
s3 -->|"append 50: full, copy 4"| s4
Step-by-step trace
Input: Append [10, 20, 30, 40, 50] to an empty dynamic array that starts with capacity 1.
| Step | append | Resize? | Elements copied | Capacity | Size | Buffer |
|---|---|---|---|---|---|---|
| 1 | 10 | no | 0 | 1 | 1 | [10] |
| 2 | 20 | yes, 1 -> 2 | 1 | 2 | 2 | [10, 20] |
| 3 | 30 | yes, 2 -> 4 | 2 | 4 | 3 | [10, 20, 30, _] |
| 4 | 40 | no | 0 | 4 | 4 | [10, 20, 30, 40] |
| 5 | 50 | yes, 4 -> 8 | 4 | 8 | 5 | [10, 20, 30, 40, 50, _, _, _] |
Result: 5 appends caused 7 element copies in total. Doubling keeps total copies below the number of appends times 2.
Complexity
| Operation | Time | Space |
|---|---|---|
Read / write a[i] |
O(1) | O(1) |
| Append (amortized) | O(1) | O(1) |
| Append (worst case, during resize) | O(n) | O(n) |
| Insert / delete in the middle | O(n) | O(1) |
| Search unsorted | O(n) | O(1) |
Python reference implementation
class DynamicArray:
"""A growable array that doubles its capacity when full."""
def __init__(self):
self.capacity = 1
self.size = 0
self.data = [None] * self.capacity
def append(self, value):
copies = 0
if self.size == self.capacity:
copies = self._resize(2 * self.capacity)
self.data[self.size] = value
self.size += 1
return copies
def _resize(self, new_capacity):
new_data = [None] * new_capacity
for i in range(self.size): # copy every existing element
new_data[i] = self.data[i]
self.data = new_data
self.capacity = new_capacity
return self.size
def __getitem__(self, index):
if not 0 <= index < self.size:
raise IndexError("index out of range")
return self.data[index]
def __len__(self):
return self.size
Common mistakes and tips
- Growing by a constant amount (say +10) instead of doubling makes n appends cost O(n^2).
- Off-by-one errors live at
len(a) - 1; write the loop bounds out loud. - In Python,
list.insert(0, x)andlist.pop(0)are O(n). Usecollections.dequefor queues.
The trace above is generated by running the code in dsa/ with python3 tools/build.py, and checked by the tests in tests/.