O(n²)
—Quadratic Time
CautionTwo nested loops. Works on small inputs, breaks on large ones.
Mental model
Comparing everyone at a party with everyone else for a handshake. 10 people → 100 handshakes. 100 people → 10,000 handshakes. Double the guests, quadruple the work.
Interactive Visualisation
🤝 Imagine everyone at a party shaking hands with everyone else. For each of the n guests (outer loop), they shake hands with all n guests (inner loop). That's n × n = n² handshakes. Double the guests? Four times the handshakes.
n:→ 5² = 25 comparisons
[0]
[1]
[2]
[3]
[4]
[0]
[1]
[2]
[3]
[4]
n² explodes fast
n=525
n=10100
n=20400
n=502500
n=10010k
n=10001M
How it scales
n = 10
100 ops
n = 100
10,000 ops
n = 1,000
1,000,000 ops
n = 10,000
100,000,000 ops