n=1n=2n=3n=41def sum_count(nums):2 s = 03 c = 04 for x in nums:5 s += x6 c += 17 return s, cn=5n=6n ≥ n₀ = 3nsteps
Towers: one block per step the function takes, one tower per input size nRings: c·g(n), a ceiling (Big O) or a floor (Big Omega)A hopping block: one counted line running onceThe Python function sum_count on the card; each counted line it runs drops one block on the tower for that input size, so tower n holds T(n) = 2n + 3 blocks. Rings mark multiples of a simple function g(n): a ceiling that holds every tower from n₀ on proves Big O, a floor proves Big Omega, both prove Big Theta. The ½n² ring rises past the towers: 2n + 3 is not Ω(n²).

Counting the work

  1. To compare algorithms we count steps, not seconds. This Python function adds up a list. Rule: each simple line costs one step every time it runs; the for line only drives the loop, so we skip it. n is how many numbers the list holds.

Ceilings and floors

Tight bounds

What happens

;; step 1/8 · count-rule
cost model: one step per simple line, each time it runs
  1  def sum_count(nums):  (not counted)
  2      s = 0             1 step
  3      c = 0             1 step
  4      for x in nums:    (not counted)
  5          s += x        1 step
  6          c += 1        1 step
  7      return s, c       1 step

example: sum_count([4, 1, 5]) returns (10, 3)
n = len(nums), the input size

The cost model counts each simple line once per run and skips the def and for lines. Counting the for line too (it is checked n + 1 times) gives 3n + 4 steps. The constants change; the growth rate does not: it is still Θ(n). The definitions follow MIT 6.006 and CLRS chapter 3: f(n) = O(g(n)) if some positive c and n₀ give f(n) ≤ c·g(n) for every n ≥ n₀; Ω flips the inequality; Θ means both. The figure checks n = 1 to 6, and the algebra (2n + 3 ≤ 3n exactly when n ≥ 3) covers every larger n.