Big-O for everyday data structures
Big-O notation describes how the cost of an operation grows as the input grows. It ignores constant factors and small inputs, and keeps only the shape of the curve. A hash map lookup is on average, so users.get(id) costs about the same with ten users or ten million. Scanning a list is , so doubling the list doubles the work.
You rarely need proofs in day-to-day work. You need a feel for which operations are cheap on which structures, and for how fast the bad cases get bad.
The growth ladder
The classes you meet most often, with what they mean when is one million:

| Class | Name | Steps at | Everyday example |
| constant | 1 | a[i], map.get(k) | |
| logarithmic | ~20 | binary search, heap push | |
| linear | one pass over a list | ||
| linearithmic | ~ | sorting | |
| quadratic | comparing every pair | ||
| exponential | more than atoms in the universe | trying every subset |
At around simple steps per second, finishes in a fraction of a second. takes hours. That gap, not the exact constant, is what Big-O is for.
Arrays and dynamic arrays
A contiguous block of memory. Python's list, JavaScript's Array, Java's ArrayList, Go slices and C++ std::vector all work this way.
| Operation | Cost | Why |
Read or write a[i] | address = start + i × size | |
| Append at the end | amortised | see below |
| Insert or delete at the front or middle | everything after it shifts | |
| Search, unsorted | check each item | |
| Search, sorted (binary search) | halve the range each step |
Why appending is amortised
When the array runs out of room, it allocates a block twice as large and copies everything across. That single append costs . But it happens only at sizes 1, 2, 4, 8 and so on, so the total copying over appends is
Spread over appends, that is a constant amount of extra work each. "Amortised" means exactly this: an occasional expensive step, paid for by many cheap ones.

Linked lists
Each node points to the next. Good at one thing: inserting or removing at a node you already hold.
| Operation | Cost |
| Insert or delete at the head | |
| Insert or delete after a node you hold | |
| Access the -th item | |
| Search |
In practice arrays often beat linked lists even where the table says otherwise, because walking scattered nodes misses the CPU cache on almost every step.
Stacks, queues and deques
- Stack (last in, first out): push and pop at one end, . A dynamic array does this well.
- Queue (first in, first out): enqueue at the back, dequeue at the front, . Use a ring buffer or a deque, not
list.pop(0)in Python, which is . - Deque: push and pop at both ends in . Python's
collections.dequeis the usual answer.
Hash maps and hash sets
A hash function turns the key into a bucket number, so the map jumps straight to the right place instead of searching.

| Operation | Average | Worst case |
| Get, put, delete | ||
| Iterate all entries | ||
| Find the smallest key |
The worst case appears when many keys land in the same bucket, either from a poor hash function or from an attacker who chooses colliding keys on purpose. Real implementations defend against it with randomised hashing, and Java switches long chains to balanced trees. When the table gets about three quarters full, it doubles and rehashes, which is amortised for the same reason array appends are.
Hash maps keep no order. If you need "the smallest key" or "all keys between 10 and 20", you want a tree.
Balanced binary search trees
Red-black trees, AVL trees and B-trees keep keys sorted and the tree height near . Java's TreeMap, C++ std::map and database indexes are built on them.
| Operation | Cost |
| Search, insert, delete | |
| Min or max | |
| Range query returning items | |
| In-order iteration |
An unbalanced BST degrades to a linked list when keys arrive in sorted order, and every operation becomes . Always use a self-balancing variant.
Heaps (priority queues)
A heap keeps the smallest (or largest) item on top without sorting the rest.
| Operation | Cost |
| Peek at the min | |
| Push | |
| Pop the min | |
| Build from items | |
| Search for an arbitrary item |
Heaps power schedulers, "top " queries, and graph algorithms like Dijkstra's shortest path.
Choosing a structure
Start from the operation that has to be fast, then pick the structure that makes it cheap:

Sorting
| Algorithm | Best | Average | Worst | Extra memory | Stable |
| Merge sort | yes | ||||
| Heap sort | no | ||||
| Quick sort | no | ||||
| Timsort (Python, Java objects) | yes | ||||
| Insertion sort | yes | ||||
| Counting sort | yes |
Any sort that works by comparing items needs comparisons in the worst case. Counting sort and radix sort get around that by not comparing at all, which only works when keys are small integers (range ) or fixed-width.
Insertion sort looks bad on paper but wins on tiny or nearly sorted inputs, which is why Timsort uses it for short runs.
Reading Big-O honestly
- Constants matter at small . An scan of 50 items in a cache-friendly array can beat an hash lookup.
- Average is not worst. Hash maps and quick sort have bad worst cases. Know when an attacker or unlucky input can trigger them.
- Amortised is not every time. An append that triggers a resize really does pause. That matters in latency-sensitive code.
- Watch hidden loops.
x in some_listinside a loop is overall. Switching to a set makes it . - Memory has a cost too. Space complexity follows the same notation and can be the real constraint.