Big O notation
Big O notation describes an asymptotic upper bound on how an algorithm’s resource usage grows with input size n. It’s a tool for reasoning about scalability, not a stopwatch.
Explore the curves
Section titled “Explore the curves”INTERACTIVE / LIVE
Live demoComplexity explorer
Move the input-size slider and enable or disable growth curves.
Common complexity classes
Section titled “Common complexity classes”The table is a quick reference. Each class describes how the work grows as the input gets bigger.
| Class | Name | Example |
|---|---|---|
O(1) |
Constant | Array element access |
O(log n) |
Logarithmic | Binary search on sorted data |
O(√n) |
Square root | Simple trial division up to √n |
O(n) |
Linear | Scanning a list |
O(n log n) |
Linearithmic | Merge sort |
O(n²) |
Quadratic | Two nested loops over an input |
O(2ⁿ) |
Exponential | Naive enumeration of subsets |
O(n!) |
Factorial | Enumerating every permutation |
Example: linear search
Section titled “Example: linear search”Here is a small C# method that checks each item until it finds the target.
static int FindIndex(int[] values, int target){ for (int i = 0; i < values.Length; i++) { if (values[i] == target) return i; }
return -1;}The worst case examines all n items, so the time complexity is O(n). It uses O(1) additional space.
Continue learning
Section titled “Continue learning”Try binary search to see how checking the middle of a sorted array reduces the remaining search range.