Skip to content
Data Structures and Algorithms
Text size 2 of 4

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.

StructureAccessSearchInsertDeleteSpace
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--
Dynamic-array insert is amortized O(1) at the end but O(n) at an interior index. Heap search and delete refer to an arbitrary key; its native ops are peek-min O(1) and extract-min O(log n).

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.

StructureAccessSearchInsertDeleteSpace
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--
Union-find bounds are amortized over a run; a single find or union with path compression and union by rank is effectively constant. A trie stores up to the alphabet size Sigma pointers per node across N total characters.

The one that pays the rent

A plain BST and a hash table both look O(log n) or O(1) until the input turns against them. A balanced BST buys the guarantee back for ordered data; a good hash function and a bounded load factor buy it back for unordered lookup. Know which worst case you can afford before you pick.

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.

AlgorithmBestAverageWorstSpaceStable
Insertion sortYes
Selection sortNo
Bubble sortYes
Merge sortYes
QuicksortNo
HeapsortNo
No comparison sort beats O(n log n) worst case. Quicksort's extra space is the O(log n) recursion stack; its O(n squared) worst case comes from a bad pivot on already-sorted input.

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 .

AlgorithmTime (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
Dijkstra assumes non-negative weights and a binary-heap priority queue. Bellman-Ford is slower but handles negative edges and flags negative cycles.

Where the numbers come from

Every bound here is derived in the lesson its row links to, from the Big-O rules through the Master Theorem. If a row surprises you, the derivation is one click away.