Big-O Notation
Growth rates and worst-case analysis.
Big-O notation describes how an algorithm's running time or memory grows as the input size grows, ignoring constant factors and lower-order terms. An algorithm that takes steps is , because for large the term dominates everything else.
Common classes from fastest to slowest growth are , , , , and . The difference is dramatic at scale: for a million items, is a trillion steps while is about twenty million.
Key terms
- Big-O
- f is O(g) if f is eventually bounded by a constant multiple of g.
Key ideas
- Drop constants and lower-order terms: is .
- Sequential steps add; nested loops multiply.
- Halving the problem each step gives .
- Big-O usually describes the worst case unless stated otherwise.
Worked example
A loop over items contains a loop over all items, followed by a separate single loop over . What is the complexity?
- Nested part: steps.
- Separate loop: steps.
- Total: .
- Keep the dominant term: .
Answer:
Common mistakes
- Adding the costs of nested loops instead of multiplying.
- Keeping constants, as in .
- Assuming an algorithm is always faster than for small inputs.
Practice questions
Try each one, then open the answer. Numerical answers on this page are verified by an automated calculation check.
1.About how many halvings take 1024 items down to 1?
Show answer
2.If an algorithm takes 1 s for , about how long for ?
Show answer
s
3.Steps for when .
Show answer