Picking a structure by its dominant operation
The right data structure depends on which operation you do most: random access, searching, or inserting and deleting. This reference lists the Big-O time complexity for each of those operations across the common structures — arrays, linked lists, hash tables, trees, heaps and graphs — plus their space cost. Search by keyword and highlight the operation you care about.
How it works
Each structure trades operations against one another. The headline complexities:
Array access O(1) search O(n) insert/delete O(n)
Linked list access O(n) search O(n) insert/delete O(1)* (*with node ref)
Hash table search/insert/delete O(1) avg, O(n) worst, no order
Balanced BST all O(log n) worst-case guaranteed
Binary heap peek O(1), push/pop O(log n)
Graph (list) traversal O(V+E), Graph (matrix) edge lookup O(1), traversal O(V²)
Average-case figures assume good conditions: a low hash load factor, a balanced tree, a reasonable distribution. Structures that can degrade — hash tables and unbalanced BSTs — are marked so you can plan for the worst case.
Tips and notes
- Dynamic-array append is amortised O(1) but occasionally O(n) when the backing buffer resizes — fine for most uses, watch it in latency-critical paths.
- Hash tables win for key lookup but give no ordering; use a balanced tree (or a B-tree on disk) when you also need sorted traversal or range queries.
- Heaps give O(1) access to the min or max but are not searchable in log time — reach for them for priority queues, not membership tests.
- Choose adjacency lists for sparse graphs (O(V+E) space) and matrices for dense graphs needing O(1) edge checks.