Counting the work
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.
Three numbers in the list. s = 0 and c = 0 cost 2 steps. The loop body runs 3 times at 2 steps each: 6 more. return adds 1. Total: 9 steps, one block each on the tower for n = 3.
Do the same for n = 1 to 6. Each extra number adds one more loop pass, so 2 more blocks: 5, 7, 9, 11, 13, 15. The count is a function of n: T(n) = 2n + 3.
Ceilings and floors
Big O asks for a ceiling: is T(n) below c times a simple function g(n), here g(n) = n? Put a ring at height 2·n on each tower. Every tower pokes 3 blocks above its ring: c = 2 is too small.
Lift the rings to 3·n. Towers 1 and 2 still poke out, but from n = 3 on every tower fits under. That is the rule: T(n) ≤ c·g(n) for all n ≥ n₀. With c = 3 and n₀ = 3, T(n) = O(n).
Big Omega flips the test: a floor. Rings at 2·n sit below the top of every tower, for every n ≥ 1, since 2n + 3 is always more than 2n. So T(n) = Ω(n): it grows at least as fast as n.
Tight bounds
Both at once: from n = 3 on, each tower sits between its floor at 2n and its ceiling at 3n. A count caught between two multiples of g(n) is Θ(g(n)). So T(n) = Θ(n): it grows like n, up to a constant.
Now try g(n) = n². Even rings at ½·n² overtake the towers at n = 6 (18 > 15), and any c·n² does sooner or later. So T(n) is O(n²), a loose ceiling, but not Ω(n²) or Θ(n²).
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.