CampusFlow
AlgorithmsTime Complexity Cheatsheet

📊 Time Complexity Cheatsheet

Quick reference for Big O notation, algorithm complexities, and performance characteristics.

Big O Notation Reference

NotationNameDescriptionExamples
O(1)ConstantFixed time regardless of input sizeArray access, hash lookup, stack push/pop
O(log n)LogarithmicTime grows logarithmically with inputBinary search, balanced BST operations
O(n)LinearTime grows proportionally to inputLinear search, array traversal, string comparison
O(n log n)LinearithmicCombination of linear and logarithmicMerge sort, heap sort, quick sort (avg)
O(n²)QuadraticTime grows quadraticallyBubble sort, insertion sort, nested loops
O(2ⁿ)ExponentialTime doubles with each input additionFibonacci (naive), tower of Hanoi
O(n!)FactorialTime grows factoriallyPermutations, traveling salesman (naive)

Growth Rate Comparison

nO(1)O(log n)O(n)O(n log n)O(n²)O(2ⁿ)
101310301001,024
1001710070010,0001.27×10³⁰
1,0001101,00010,0001,000,000
10,00011310,000130,000100M

Common Algorithm Complexities

AlgorithmBestAverageWorstSpace
Array AccessO(1)O(1)O(1)O(1)
Binary SearchO(1)O(log n)O(log n)O(1)
Linear SearchO(1)O(n)O(n)O(1)
Bubble SortO(n)O(n²)O(n²)O(1)
Merge SortO(n log n)O(n log n)O(n log n)O(n)
Quick SortO(n log n)O(n log n)O(n²)O(log n)
Heap SortO(n log n)O(n log n)O(n log n)O(1)
Insertion SortO(n)O(n²)O(n²)O(1)
Selection SortO(n²)O(n²)O(n²)O(1)
BFS/DFS (Graph)O(V+E)O(V+E)O(V+E)O(V)
DijkstraO(V+E log V)O(V+E log V)O(V+E log V)O(V)
Hash TableO(1)O(1)O(n)O(n)
BST OperationsO(log n)O(log n)O(n)O(1)

💡 Quick Tips

  • 1. Always consider input constraints when choosing algorithms. n ≤ 20 may allow O(2ⁿ), but n ≥ 10⁶ needs O(n) or better.
  • 2. O(n log n) is often considered the "gold standard" for sorting - much faster than O(n²) for large inputs.
  • 3. Space-time tradeoff: sometimes using O(n) extra space can reduce time complexity significantly (e.g., hash tables).
  • 4. Best case complexity is rarely useful - focus on average and worst-case performance.
  • 5. Constants matter! O(100n) is still O(n), but an O(n) algorithm with high constant may be slower than O(n log n) for practical inputs.