Watch step counts explode across a grid as n grows — and see why halving beats counting every single time.
The big picture
Why Big-O exists
Big-O is a promise about growth: how does the cost of an algorithm stretch as the input gets bigger? Constants and small inputs stop mattering; the shape is everything.
1
What Big-O means
An upper bound on growth. O(n) says: double the input, roughly double the work — no matter what the input looks like.
2
Drop the constants
O(2n) and O(100n) are both written O(n). At scale, the multiplier is noise; only the shape survives.
3
The pecking order
O(1) < O(log n) < O(n) < O(n log n) < O(n²). Each step up is a catastrophic jump: 1,048,576 items cost 20 probes vs 1.1 trillion for n².
4
Worst case rules
Big-O usually quotes the worst case: the target missing, the array reversed. Best cases are luck; promises must hold always.
5
Space counts too
Time is steps, space is extra memory. Bubble sort is O(1) space; merge sort needs an O(n) auxiliary array.
Why Big-O exists
What Big-O means1An upper bound on growth. O(n) says: double the input, roughly double the work — no matter what the input looks like.
Drop the constants2O(2n) and O(100n) are both written O(n). At scale, the multiplier is noise; only the shape survives.
The pecking order3O(1) < O(log n) < O(n) < O(n log n) < O(n²). Each step up is a catastrophic jump: 1,048,576 items cost 20 probes vs 1.1 trillion for n².
Worst case rules4Big-O usually quotes the worst case: the target missing, the array reversed. Best cases are luck; promises must hold always.
Space counts too5Time is steps, space is extra memory. Bubble sort is O(1) space; merge sort needs an O(n) auxiliary array.
5 building blocks make up Why Big-O exists
Algorithm in motion
Growth at scale
One grid, three input sizes, four growth rates — watch the columns split apart as n doubles and doubles again.
log₂ nnn·log₂ nn²
n = 8————
1 / 13
Step counts for four growth rates, filled in as n explodes from 8 to a million.
Visualize
Naming the cost
Reading Big-O off a snippet: look at the loops, keep the fastest-growing term.
process decision input / output start / end
Watch it run
Walkthrough
Two counting programs: a halving loop that logs, and a nested loop that squares.
Halving.java
1publicclass Halving {
2publicstaticvoidmain(String[]args){
3int size =16;
4int halvings =0;
5while(size >1){
6 size = size /2;
7 halvings++;
8}
9System.out.println(halvings +" halvings");
10}
11}
main
size = 16 — the problem starts big.
Memory
size= 16
Console
output appears as the program runs…
Step 1/8
Algorithm in motion
Best vs worst case
Same algorithm, same array — the target’s position alone decides whether you pay for one read or all of them.
64
0
i
87
1
52
2
91
3
23
4
78
5
10
6
45
7
1 / 10
Hunting 78 again — this time to see how its hiding place changes the bill.
Challenge
Predict the output
A triangular loop and a halving loop — count the trips by hand.
Predict.java
1publicclass Predict {
2publicstaticvoidmain(String[]args){
3int count =0;
4for(int i =1; i <=3; i++){
5for(int j =1; j <= i; j++){
6 count++;
7}
8}
9System.out.println(count);
10int x =100;
11int halvings =0;
12while(x >1){
13 x = x /2;
14 halvings++;
15}
16System.out.println(halvings);
17}
18}
Your predicted output — one line per console line
Hint: The inner loop’s bound is i, not a constant: 1 + 2 + 3 trips. And 100 keeps halving while it is still greater than 1.
Practice
Concept check
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand
?Big-Odrops into place
?O(1)drops into place
?O(log n)drops into place
?O(n)drops into place
?O(n²)drops into place
?worst casedrops into place
?space complexitydrops into place
?drop constantsdrops into place
Recap
Key takeaways
Key takeaways6 takeaways
01
Big-O measures growth, not speed on one input — constants vanish, the shape remains.
02
Ranking to memorize: O(1)<O(log n)<O(n)<O(n log n)<O(n²).
03
A loop that halves the problem each trip is O(log n) — 1,048,576 needs just 20 halvings.
04
Loops stacked inside loops multiply: n trips × n trips= n².
05
Worst case is the honest promise: target missing, array reversed, everything unlucky.
06
ArrayList.add is amortized O(1) — the occasional grow-and-copy spreads thin across cheap adds.