Start with a comparison
Compare looking up the first list item with checking every item. What changes when the list doubles in length?
Review Big O notation with the class handout and textbook pages 358–361. Compare how time and auxiliary space grow with input size.
Watch the number of marked operations as input size changes.
1visits = []23def growth_linear(n):4 for i in range(n):5 visits.append(i)6 return len(visits) visits is an instrumentation list used only to show work. Do not count this display logging when judging the underlying algorithm’s auxiliary space. We count append events, not every Python operation.
Compare a running total with a growing copy of the input.
1def auxiliary_space(items, copy_input):2 total = 03 working = []4 for value in items:5 total = total + value6 if copy_input:7 working.append(value)8 return total The supplied list is input space. total and value occupy a fixed number of slots in our classroom model. The copy grows to n elements. Without the copy: O(1) auxiliary space; with it: O(n). Both scans take O(n) time.
Compare looking up the first list item with checking every item. What changes when the list doubles in length?
Count the work in a single loop over n values, two consecutive loops, and two nested loops. Try n = 4 and n = 8 before generalizing.
Connect constant, logarithmic, linear and quadratic growth to the algorithms you have met. Distinguish growth rate from measured execution time.
Compare linear search with binary search. State the case being considered and explain the role of sorted input. Discuss auxiliary space separately from time.
Explain why doing two full passes over a list still has linear growth rather than quadratic growth.
Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.
A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.
Count a single loop for n = 4 and n = 8. Predict the effect of doubling n.
No imports needed.
Change one value, predict the output, then run and explain any difference.
# Add one to the counter each time this line runs.
n = 4
count = 0
for i in range(n):
count += 1
print(count)
Count nested-loop operations and compare them with two consecutive loops.
No imports needed.
Nested loops repeat the inner work for every outer iteration.
# Nested loops repeat the inner work for every outer iteration.
n = 4
count = 0
for i in range(n):
for j in range(n):
count += 1
print(count, 2 * n)
If an input doubles, what happens to the work in a linear algorithm and a quadratic algorithm?
Review the Big O lesson by reading pages 358–361 in Computer Science for the IB Diploma by Baumgarten, Ganea & Turland. Next: binary search.