A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Big-O Notation and Complexity Analysis

College Depth 83 in the knowledge graph ☐ I know this ☆ Set as goal
342topics build on this
399prerequisites beneath it
See this on the map →
Asymptotic Notation: Big-O, Big-Omega, Big-Theta→→Amortized AnalysisExternal Memory Algorithms+14 more
complexity analysis big-o asymptotics

Core Idea

Big-O notation provides an upper bound on how an algorithm's runtime grows with input size, focusing on asymptotic behavior while ignoring constant factors and lower-order terms. It enables meaningful algorithm comparison independent of hardware. For example, linear search is O(n) while binary search is O(log n), making binary search vastly superior for large inputs despite higher constant factors.

How It's Best Learned

Start with concrete examples by counting operations in simple loops and recursive functions, identifying the dominant term. Practice deriving Big-O for nested loops, divide-and-conquer recurrences, and data structure operations before moving to general complexity classes.

Common Misconceptions

Explainer

You already understand from algorithm design basics that algorithms can be compared by how efficiently they solve a problem, and from asymptotic notation that Big-O provides an upper bound on growth rate. This topic shifts focus from the mathematical definition to the practical skill of analyzing code to determine its Big-O complexity. The goal is to look at an algorithm — its loops, recursive calls, and data structure operations — and derive how its runtime scales with input size n.

The fundamental technique is counting the dominant operation. For a single loop that iterates n times and does constant work per iteration, the complexity is O(n). For two nested loops that each iterate n times, the inner loop executes n times for each of the n outer iterations, giving O(n²). If the inner loop depends on the outer variable — say, iterating from i to n — the total work is n + (n-1) + ... + 1 = n(n+1)/2, which is still O(n²) because we drop the constant factor and lower-order term. The rule is always the same: identify the term that grows fastest and discard everything else.

Recursive algorithms require a different approach. Consider binary search: each recursive call halves the input and does constant work, giving the recurrence T(n) = T(n/2) + O(1). This solves to O(log n). Merge sort splits the input in half but does O(n) work to merge, giving T(n) = 2T(n/2) + O(n), which solves to O(n log n). You do not need to memorize these — the pattern is to write the recurrence, then either expand it, use the Master Theorem, or draw a recursion tree to see how the total work accumulates across levels. The key insight is that dividing the problem in half at each step is the signature of logarithmic depth, while doing linear work at each level multiplies that depth by n.

In practice, Big-O analysis is a starting point, not the final answer. Two O(n log n) sorting algorithms — merge sort and quicksort — have very different real-world performance because quicksort has better cache behavior and smaller constant factors. An O(n) algorithm with a constant factor of 1000 will lose to an O(n log n) algorithm for inputs under about 10,000. The power of Big-O is in ruling out bad choices at scale: if your dataset has a billion elements, no O(n²) algorithm is viable regardless of constant factors, while O(n log n) is comfortable. Use Big-O to narrow the field, then benchmark the survivors on your actual data and hardware.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Asymptotic Notation: Big-O, Big-Omega, Big-Theta → Big-O Notation and Complexity Analysis

Longest path: 84 steps · 399 total prerequisite topics

Prerequisites (1)

Leads To (16)