Algo Lab
← Raymond Pham

Big-O Notation

01FoundationsLesson 1 of 4
4091227354158667n = 8 items
In this lesson←→ to stepStep 1 of 9
Step 1 / 9Concept

Counting steps, not seconds

A stopwatch tells you how fast one program ran on one machine. It says nothing about what happens when the input is ten times bigger.

Big-O counts basic steps as a function of the input size n. Here n = 8, and every cell is one item an algorithm might have to look at.

What changed

  • +4
  • +9
  • +2
  • +7
  • +5
  • +1

←→ step · R replay · Space play

Pseudocode

1analyze(items):2    x = items[3]                  # O(1): one step3    for each item in items:       # O(n): touch every item4        total = total + item5    while size > 1:               # O(log n): halve each time6        size = size / 27    for each a in items:          # O(n^2): loop inside a loop8        for each b in items:9            compare(a, b)10    return total

By the numbers

  • n8
  • Steps—
  • Growth—
  • Extra space—