Labs

Loops in motion

Watch five classic loops run step by step: each step is a square, and the shape they draw is the Big-O.

Loop
count = 0
for i in range(n):
    for j in range(i + 1, n):
        if a[i] + a[j] == target:
            count += 1
return count
1/30

target = 69. Check every pair i < j.

Counted steps0O(n²)
At the end28n = 8

A triangle, half an n × n square: quadratic.

O(n²): at n = 8 it is 28 steps. Double n and see: a row doubles, a triangle grows about 4×, the halving column grows by one.

How it's calculated · assumptions

Counted step = one comparison, probe or write. Small arrays so every step is visible; the shapes (and curves) are what scale.