Skip to content
  • Open the math solver
  • Ask the AI tutor
  • Find an AP course
  • Create a quiz
  • Open the calculator
  • Graphing Calculator
  • Browse all calculators
  • Read math guides
↑ ↓Enter to open · Esc to close
Solve a problem
AlgorithmsAlgorithmsCollege

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 nn grows, ignoring constant factors and lower-order terms. An algorithm that takes 3n2+5n+23n^2 + 5n + 2 steps is O(n2)O(n^2), because for large nn the n2n^2 term dominates everything else.

Common classes from fastest to slowest growth are O(1)O(1), O(log⁡n)O(\log n), O(n)O(n), O(nlog⁡n)O(n \log n), O(n2)O(n^2) and O(2n)O(2^n). The difference is dramatic at scale: for a million items, n2n^2 is a trillion steps while nlog⁡2nn \log_2 n is about twenty million.

Key terms

Big-O
f is O(g) if f is eventually bounded by a constant multiple of g.
f(n)≤C g(n) for n≥n0f(n) \le C\,g(n) \text{ for } n \ge n_0

Key ideas

  • Drop constants and lower-order terms: 5n+1005n + 100 is O(n)O(n).
  • Sequential steps add; nested loops multiply.
  • Halving the problem each step gives O(log⁡n)O(\log n).
  • Big-O usually describes the worst case unless stated otherwise.

Worked example

A loop over nn items contains a loop over all nn items, followed by a separate single loop over nn. What is the complexity?

  1. Nested part: n×n=n2n \times n = n^2 steps.
  2. Separate loop: nn steps.
  3. Total: n2+nn^2 + n.
  4. Keep the dominant term: O(n2)O(n^2).

Answer: O(n2)O(n^2)

Common mistakes

  • Adding the costs of nested loops instead of multiplying.
  • Keeping constants, as in O(2n)O(2n).
  • Assuming an O(n)O(n) algorithm is always faster than O(n2)O(n^2) 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. 1.About how many halvings take 1024 items down to 1?

    Show answer

    1010

  2. 2.If an O(n2)O(n^2) algorithm takes 1 s for n=1000n = 1000, about how long for n=3000n = 3000?

    Show answer

    99 s

  3. 3.Steps for nlog⁡2nn \log_2 n when n=256n = 256.

    Show answer

    20482048

More in Algorithms