Sub topic 6
What each operation costs
This chapter assumes you know Big-O notation and have seen the array-backed Stack and the LinkedStack from the previous chapters. It compares what their operations cost.
The cost table
Both implementations expose the same four operations. Their costs differ only in how push behaves.
| Operation | Array-backed stack | Linked-list stack |
|---|---|---|
| `push` | O(1) amortized, O(n) worst case | O(1) worst case |
| `pop` | O(1) | O(1) |
| `peek` | O(1) | O(1) |
| `is_empty` | O(1) | O(1) |
| Space for n items | O(n) | O(n), plus one pointer per node |
pop, peek, and is_empty only touch the top of the stack. Their cost does not depend on how many items the stack holds.
Why array push is "amortized" O(1)
A dynamic array has a fixed capacity at any moment. When it is full, an append allocates a larger block and copies every item into it. That single push costs O(n).
The array grows by a constant factor, such as doubling, so full-array copies become rarer as the stack grows. Amortized analysis averages the cost over a whole sequence of operations. After n pushes, the total copying is bounded by a constant multiple of n, so the average cost per push is O(1).
The script below simulates a doubling array and counts how many items get copied.
def count_copies(n):
capacity = 1
size = 0
copies = 0
for _ in range(n):
if size == capacity:
copies += size # copy every item into a bigger block
capacity *= 2
size += 1
return copies
for n in (10, 1000, 100000):
copies = count_copies(n)
print(n, copies, copies / n)It prints this:
10 15 1.5
1000 1023 1.023
100000 131071 1.31071The copy count never reaches 2n, however large n gets. Python's list grows by a smaller factor than 2, but the argument still holds for any constant factor above 1.
Worst case versus amortized
Amortized O(1) is not a bound on each individual call. One push can still be slow. This matters when you need predictable latency, such as in a real-time loop where a single pause is unacceptable.
The linked stack has no resize step. Each push allocates one node and relinks the head, so every operation is O(1) in the worst case. The price is a pointer in every node and an allocation on every push. In practice, the array-backed stack is usually faster because its items sit next to each other in memory, which suits the CPU cache.
Space
Both stacks use O(n) space. The array-backed stack may hold unused capacity, since a list that just grew can be nearly half empty. The linked stack uses exactly one node per item, but each node is larger than a bare array slot.
A common mistake: using the wrong end
The O(1) bounds depend on putting the top of the stack at the end of the list.
items = []
items.insert(0, "x") # O(n): shifts every existing item
items.pop(0) # O(n): shifts every remaining itemIf you make the front of the list the top, each push and pop shifts every item. A loop of n operations then costs O(n²). Use append and pop() with no argument.
Choosing between them
- Choose the array-backed stack by default. It is simpler, uses less memory per item, and has good cache behavior.
- Choose the linked stack when you cannot tolerate an occasional O(n) push, or when you want stack operations that never move existing data.
Exercise
An array-backed stack doubles its capacity when full. You push 1,000 items starting from capacity 1. Roughly how many item copies happen in total, and what does that imply for the average cost of a push?
Show answer
The resizes copy 1 + 2 + 4 + ... + 512 = 1,023 items in total, which is less than 2n. The average cost per push is therefore a small constant, so push is amortized O(1), even though the single push that triggers a resize costs O(n).