On this page
Complexity & Big-O Notation
Prev: algorithms_and_dsa | Up: algorithms_and_dsa | Next: Linear & Hashed Structures
Rel: Pointers & CPU Cache Locality
Ref: Wikipedia — Big O Notation
Tags: #public #prog #programming #algorithms #big-o
Contents
Common Growth Classes
Big-O describes how runtime or memory scales as input size $n \to \infty$ (dropping constant factors and lower-order terms):
| Complexity | Name | Typical Example |
|---|---|---|
| $O(1)$ | Constant | Array index lookup, hash table lookup (avg) |
| $O(\log n)$ | Logarithmic | Binary search, balanced BST / B-Tree lookup |
| $O(n)$ | Linear | Single pass over an array or list |
| $O(n \log n)$ | Linearithmic | Merge sort, Timsort, heapsort |
| $O(n^2)$ | Quadratic | Nested loops comparing all pairs |
| $O(2^n)$ | Exponential | Unmemoized recursive subsets / naive Fibonacci |
Amortized Complexity & Cache Reality
- Amortized $O(1)$: Dynamic array
appendoccasionally resizes in $O(n)$ (doubling capacity), but averaged over $n$ appends, each costs $O(1)$. - Constant factors matter: For small $n$, a contiguous $O(n)$ array scan in L1 cache often beats an $O(\log n)$ pointer-heavy tree lookup. Always profile before micro-optimizing.
Linked from
Related notes
- tuts/programming/algorithms_and_dsa Linked here; links to this page; shared tags: #algorithms, #prog, #programming
- tuts/programming/algorithms_and_dsa/linear_and_hashed_structures Linked here; links to this page; shared tags: #prog, #programming
- tuts/programming/algorithms_and_dsa/problem_solving_patterns links to this page; shared tags: #algorithms, #prog, #programming
- tuts/programming/memory_and_execution/pointers_references_and_indirection Linked here; shared tags: #prog, #programming
- tuts/cpp/stdlib/algorithm_and_ranges shared tags: #algorithms, #prog
- tuts/cpp/stdlib/algorithm_and_ranges/find_if shared tags: #algorithms, #prog
- tuts/cpp/stdlib/algorithm_and_ranges/sort shared tags: #algorithms, #prog
- tuts/cpp/stdlib/algorithm_and_ranges/transform shared tags: #algorithms, #prog
- tuts/programming shared tags: #prog, #programming
- tuts/programming/algorithms_and_dsa/trees_heaps_and_graphs shared tags: #prog, #programming