π Big-O Notation¶
Big-O is a way to describe how runtime (or memory) grows with input size n.
It ignores constant factors and focuses on long-term growth.
β Common Big-O Classes¶
| Big-O | Name | Example |
|---|---|---|
O(1) |
constant | dict/set membership (average) |
O(log n) |
logarithmic | binary search |
O(n) |
linear | scanning a list |
O(n log n) |
linearithmic | merge sort |
O(n^2) |
quadratic | nested loops over list |
β How to Analyze¶
- Loops add up:
forovernβO(n) - Nested loops multiply:
ninsidenβO(n^2) - Sequential steps: keep the dominant term:
O(n) + O(n^2)βO(n^2)
π Key Takeaways¶
- Big-O is about growth, not exact time.
- Use it to compare approaches.
- Donβt guess: reason about loops, recursion, and data structure operations.