Skip to content
dsa/roadmap
Cheat sheet

Big-O master table

1<log n<n<n log n<n²<n³<2ⁿ<n!

Sorting

SortBestAverageWorstExtra spaceStableIn-place
SelectionO(n²)O(n²)O(n²)O(1)noyes
Bubble (with flag)O(n)O(n²)O(n²)O(1)yesyes
InsertionO(n)O(n²)O(n²)O(1)yesyes
Shell (n/2 gaps)O(n log n)O(≈ n^1.5)O(n²)O(1)noyes
MergeO(n log n)O(n log n)O(n log n)O(n)yesno
QuickO(n log n)O(n log n)O(n²)O(log n)noyes
HeapO(n log n)O(n log n)O(n log n)O(1)noyes

Data structure operations

StructureAccessSearchInsertDelete
ArrayO(1)O(n)O(n)O(n)
Sorted arrayO(1)O(log n)O(n)O(n)
Singly linked listO(n)O(n)O(1) at headO(1) at head
Doubly linked listO(n)O(n)O(1) at either endO(1) given the node
Stack––O(1) pushO(1) pop
Queue (circular / list)––O(1) enqueueO(1) dequeue
BST (balanced / skewed)–O(log n) / O(n)O(log n) / O(n)O(log n) / O(n)
Max-heapO(1) maxO(n)O(log n)O(log n) max
Hash table (avg / worst)–O(1) / O(n)O(1) / O(n)O(1) / O(n)

Graphs: BFS and DFS are O(V + E) with an adjacency list, O(V²) with a matrix. Matrix space O(V²), list space O(V + E).

Every topic

1.1 What Is a Data Structure?
Array: read item iO(1)
Array: insert at the frontO(n): shift everything
Linked list: insert at the frontO(1)
Linked list: read item iO(n): walk from the head
1.2 ADTs & Abstraction
push / pop / top (array implementation)O(1)
push / pop / top (linked implementation)O(1)
Maximum sizearray: fixed · linked list: limited only by memory
1.3 Arrays & 2-D Arrays (Review)
Access a[i]O(1)
Search (unsorted)O(n)
Insert / delete at position iO(n): shift items
Insert / delete at the endO(1) if there is space
Traverse an R × C matrixO(R·C)
2.1 Algorithms & Their Properties
findMax: comparisonsexactly n − 1
findMax: timeO(n), and best = worst
2.2 Measuring Efficiency
Search with late terminationalways n checks
Search that breaks early1 … n checks
Duplicate check with nested loopsO(n²) time, O(1) space
Duplicate check with a setO(n) time, O(n) space ← trade-off
2.3 Counting Time Units
Single loop for(i=0;i<n;i++) k=k+1; cout<<k;T(n) = 4n + 3 → O(n)
Nested n × n loop with k++T(n) = 3n² + 4n + 3 → O(n²)
Loop where i doubles (i *= 2)≈ log₂ n iterations → O(log n)
2.4 Best, Average & Worst Case
Linear search: best (key is first)O(1): 1 comparison
Linear search: average (key present)(n + 1) / 2 comparisons → O(n)
Linear search: worst (last or missing)n comparisons → O(n)
Bubble sort with flag: best / worstO(n) / O(n²)
2.5 Asymptotic Notation & Big-O Proofs
O(1)constant: array access, push/pop
O(log n)logarithmic: binary search
O(n)linear: linear search, traversal
O(n log n)linearithmic: merge sort, heap sort
O(n²)quadratic: bubble, selection, insertion sort
O(n³)cubic: three nested loops
O(2ⁿ)exponential: all subsets, naive Fibonacci
3.1 Selection Sort
Best caseO(n²): it still scans for the minimum every pass
Average caseO(n²)
Worst caseO(n²): n(n−1)/2 comparisons
Swapsat most n − 1 (the fewest of the simple sorts)
3.2 Bubble Sort
Best case (already sorted)O(n): one pass, no swaps, stop
Average caseO(n²)
Worst case (reversed)O(n²): n(n−1)/2 comparisons and swaps
3.3 Insertion Sort
Best case (already sorted)O(n): n − 1 comparisons, 0 shifts
Average caseO(n²)
Worst case (reversed)O(n²): n(n−1)/2 comparisons and shifts
3.4 Pointer Sort
Comparisonssame as the underlying sort (here selection: n(n−1)/2)
Data moved per swapone pointer (8 bytes) instead of one whole record
TimeO(n²) with selection sort; O(n log n) with a fast sort
3.5 Linear Search
Best caseO(1): key at index 0
Average case (found)(n + 1)/2 comparisons → O(n)
Worst caseO(n): last item or not present
Needs sorted data?No
3.6 Binary Search
Best caseO(1): key is at the first mid
Average / worst caseO(log n): about ⌊log₂ n⌋ + 1 comparisons
1,000,000 itemsat most 20 comparisons
Needs sorted data?Yes, and random access (an array, not a linked list)
3.7 Recursion Primer
factorial(n): timeO(n): n calls, O(1) work each
factorial(n): spaceO(n) stack frames
recursive binary search: timeO(log n)
recursive binary search: spaceO(log n) stack frames (iterative version: O(1))
4.1 Pointers, new/delete & the Heap
&x, *p, p->fieldO(1)
new / delete one objectO(1) (amortised; done by the allocator)
Stack memoryautomatic: freed when the function returns
Heap memorymanual: lives until delete
4.2 Singly Linked List
Insert at frontO(1)
Insert at endO(n), or O(1) with a tail pointer
Insert at position kO(k)
Delete by value / searchO(n)
Access the i-th itemO(n): no random access
4.3 Doubly Linked List
Insert / delete at front or end (with tail)O(1)
Delete a node you already have a pointer toO(1): no need to find prev
Search / delete by valueO(n)
Traverse backwardO(n): impossible in a singly list without extra memory
5.1 Stacks with an Array
pushO(1)
popO(1)
peek / isEmpty / isFullO(1)
search for a valueO(n): not a stack operation
5.2 Stacks with a Linked List
push (insert at head)O(1)
pop (delete head)O(1)
peek / isEmptyO(1)
5.3 Queues with an Array
enqueue (at rear)O(1)
dequeue, naive (shift left)O(n)
dequeue, with front indexO(1)
front / isEmptyO(1)
5.4 Queues with a Linked List
enqueue (insert after rear)O(1)
dequeue (delete front)O(1)
peek / isEmptyO(1)
5.5 Circular Queue
enqueueO(1)
dequeueO(1)
front / isEmpty / isFullO(1)
5.6 std::stack, std::queue & std::deque
stack push / pop / topO(1)
queue push / pop / front / backO(1)
deque push/pop at either endO(1)
deque d[i]O(1)
6.1 Binary Trees & Tree Terms
count nodes / leavesO(n): visits every node once
heightO(n)
find a value (no ordering)O(n)
6.2 Binary Search Tree
search / insert, balancedO(log n)
search / insert, skewedO(n)
min / maxO(h)
inorder (sorted output)O(n)
6.3 BST Delete (3 Cases)
delete, balancedO(log n)
delete, skewedO(n)
6.4 Tree Traversals
pre / in / post orderO(n): every node once
level orderO(n)
6.5 General Trees (Left-Child, Right-Sibling)
visit all nodesO(n)
find the k-th childO(k)
count the children of a nodeO(degree)
7.1 Graph Terminology
max edges, undirectedV(V − 1)/2
max edges, directedV(V − 1)
sum of all degrees2E (each edge counted at both ends)
7.2 Adjacency Matrix & Adjacency List
spacematrix O(V²) · list O(V + E)
is there an edge u–v?matrix O(1) · list O(deg u)
visit all neighbours of umatrix O(V) · list O(deg u)
add an edgematrix O(1) · list O(1)
7.3 Directed Graphs
out-degree of ulist O(1) (size of adj[u]) · matrix O(V) (row sum)
in-degree of ulist O(V + E) · matrix O(V) (column sum)
spacematrix O(V²) · list O(V + E)
7.4 BFS & DFS
BFS / DFS with adjacency listO(V + E)
BFS / DFS with adjacency matrixO(V²)
8.1 Shell Sort
Best case (sorted)O(n log n)
Average casedepends on the gaps; about O(n^1.5) for n/2, n/4, …
Worst case (gaps n/2, n/4, …)O(n²)
8.2 Quick Sort
Best case (pivot splits in half)O(n log n)
Average caseO(n log n)
Worst case (sorted input, last-item pivot)O(n²)
8.3 Merge Sort
Best caseO(n log n)
Average caseO(n log n)
Worst caseO(n log n)
8.4 Heap Sort
Build heapO(n)
Each extract-max (swap + heapify)O(log n)
Best / average / worstO(n log n)
8.5 Hashing Basics
search / insert / delete, averageO(1)
search / insert / delete, worst (all keys collide)O(n)

Keyboard shortcuts

⌘K /
Search topics, glossary, cheat sheets
space
Play / pause the visualizer
← →
Step back / forward
r
Reset the visualizer
?
Show this help