Reference shelf
Big-O cheat sheet
Every structure and algorithm, average and worst case, plus space.
The whole course as one lookup table: every structure and every algorithm with its cost. Come back to it before an interview, or when you are choosing what to reach for.
How to read these
is the number of elements; for graphs and count vertices and edges, and for strings is the pattern length and a key's length. Every bound drops constants and lower-order terms. A dash means the operation is not native to that structure. Insert and delete costs assume you already hold a handle to the position, so they exclude the search to find it.
Data structures, average case
The cost you get in normal use, when hashes spread and trees stay balanced. This is the table to reason with day to day.
| Structure | Access | Search | Insert | Delete | Space |
|---|---|---|---|---|---|
| Array | |||||
| Dynamic array | |||||
| Linked list | |||||
| Stack | |||||
| Queue | |||||
| Deque | |||||
| Hash table | - | ||||
| Binary search tree | |||||
| Balanced BST (AVL, red-black) | |||||
| B-tree | |||||
| Binary heap | - | ||||
| Trie | |||||
| Union-find | - | - |
Data structures, worst case
The guarantee when adversarial input piles every key into one bucket or skews a plain BST into a list. This is the table to design against.
| Structure | Access | Search | Insert | Delete | Space |
|---|---|---|---|---|---|
| Array | |||||
| Dynamic array | |||||
| Linked list | |||||
| Stack | |||||
| Queue | |||||
| Deque | |||||
| Hash table | - | ||||
| Binary search tree | |||||
| Balanced BST (AVL, red-black) | |||||
| B-tree | |||||
| Binary heap | - | ||||
| Trie | |||||
| Union-find | - | - |
The one that pays the rent
Sorting algorithms
The sorts from the sorting lesson, with the three properties that decide which one fits: whether it is stable, whether it works in place, and how it behaves on already-ordered input.
| Algorithm | Best | Average | Worst | Space | Stable |
|---|---|---|---|---|---|
| Insertion sort | Yes | ||||
| Selection sort | No | ||||
| Bubble sort | Yes | ||||
| Merge sort | Yes | ||||
| Quicksort | No | ||||
| Heapsort | No |
Search, graph, and string algorithms
The rest of the algorithmic toolkit. Graph bounds are in terms of vertices and edges; string bounds in text length and pattern length .
| Algorithm | Time (average) | Time (worst) | Space |
|---|---|---|---|
| Binary search | |||
| BFS | |||
| DFS | |||
| Topological sort | |||
| Dijkstra (binary heap) | |||
| Bellman-Ford | |||
| Minimum spanning tree (Kruskal) | |||
| Minimum spanning tree (Prim) | |||
| Naive string match | |||
| KMP | |||
| Rabin-Karp |
Where the numbers come from