Big-O Notation
An asymptotic notation describing how an algorithm's worst-case runtime or memory grows as input size N scales
Table of Contents
Structure Diagram
What is Big-O Notation?
An asymptotic notation describing how an algorithm's worst-case runtime or memory grows as input size N scales. It is a core concept in Algorithms & Complexity within CS.
Core Principles
Big-O Notation provides a hardware- and language-independent way to measure how an algorithm scales as input size N grows. By dropping constant multipliers and lower-order terms (so 3N² + 20N + 5 simplifies to O(N²)), it isolates the dominant growth curve under worst-case conditions.
From fastest to slowest, common complexity tiers are O(1) (constant) < O(log N) (logarithmic) < O(N) (linear) < O(N log N) (linearithmic) < O(N²) (quadratic) < O(2^N) (exponential). For 50 items the gap is invisible, but at 100,000 items O(N log N) finishes in milliseconds while O(N²) grinds a server to a halt.
Practical Takeaways
- Flatten hidden nested loops with Hash Maps: Calling
.includes()or.find()inside a.map()orforloop quietly createsO(N×M)behavior. Pre-indexing lookups into aSetorMapcuts total work toO(N+M). - Leverage time-space tradeoffs: Caching, memoization, and database indexes trade extra
O(N)memory to collapse repeatedO(N)orO(N²)scans intoO(1)orO(log N). - Remember constants for small
N: Big-O describes asymptotic growth. For tiny arrays (N < 32), a cache-friendlyO(N)linear scan often outperforms a hash map or balanced tree in real wall-clock time.
Notable Examples
O(1): Direct array indexing (arr[i]) and hash table lookups.O(log N): Binary search on sorted arrays and B-Tree database index lookups.O(N log N): Production sorting algorithms such as Timsort and Merge Sort.O(N²): Naive all-pairs collision checks—the exact reason game engines use spatial hashing or quadtrees.