Big-O Notation
Big-O notation describes how an algorithm's run time scales with input size .
| Big-O | Name | Example |
|---|---|---|
| Constant | Array index access | |
| Logarithmic | Binary search | |
| Linear | Single loop | |
| Linearithmic | Merge sort | |
| Quadratic | Nested loops | |
| Exponential | Brute-force subsets |
Space complexity measures memory usage by this exact same logic.
Scaling and Pitfalls
To determine complexity: single loop is , nested loop is , and halving the input is .
Practical example: Computing pairwise distances between particles uses nested loops, yielding . Advanced methods like Barnes-Hut reduce this to using spatial trees.
Common pitfall: Big-O hides constants. An method can beat an one on small inputs. Complexity classes predict scaling, not micro-benchmarks.