If these ideas are new, begin with:
Understand the idea
Define input size and the dominant operation first. Count how often it executes, then simplify growth. Sequential loops add their work; dependent nested loops require summing their lengths.
Work through an example
For i from 0 to n-1 and j from 0 to i-1, iterations total n(n-1)/2. At n=8 this is 28, not 64, but both expressions grow quadratically.
Time and space complexity
The triangular loop is Θ(n²) time and O(1) auxiliary state when counting directly.
Why it works
Every legal pair (j,i) with j<i is visited once.
Common mistakes
- Two adjacent loops are not automatically quadratic.
- A library call inside a loop may hide linear work.
Chapter lab · Python implementation
Analyze growth before optimizing constants. The lab separates direct work, nested work, and repeated halving. Count iterations explicitly so asymptotic claims can be checked. Unless stated otherwise, n is input length, arithmetic fits a machine word, and auxiliary space excludes input but includes temporary collections.
This shared lab connects the chapter's topics. Follow the current lesson's worked example first, then trace the complete implementation. Additional algorithms described in topic exercises are variations you can implement independently.
def counts(n):
if n < 0:
raise ValueError('n must be nonnegative')
linear = sum(1 for _ in range(n))
pairs = sum(1 for i in range(n) for j in range(i))
halvings, x = 0, n
while x > 1:
x //= 2
halvings += 1
return linear, pairs, halvings
def doubling_copies(n):
size, capacity, copied = 0, 1, 0
for _ in range(n):
if size == capacity:
copied += size
capacity *= 2
size += 1
return copiedRun and test the lab
Copy the implementation and these assertions into a Python 3 file and run it. An assertion failure means the actual result differs from the expectation. Add the current topic's exercise and boundary tests to deepen your understanding.
assert counts(0) == (0,0,0)
assert counts(8) == (8,28,3)
assert counts(5) == (5,10,2)
assert doubling_copies(9) == 15
assert all(doubling_copies(n) < 2*n for n in range(1,100))