Big-O notation
Counting how work grows with n, and ignoring everything else.
Before you start
Two algorithms solve the same problem. Which is better? "Faster on my laptop" is not a good answer — a faster laptop or a smaller input can flip the result.
**Big-O notation** answers the question in a machine-independent way: how does the amount of work grow as the input gets bigger? O(n) means work grows in step with input. O(n²) means work grows with the *square* of input — double the input, quadruple the work.
This lesson shows five common growth shapes side by side, watches their operation counts as n doubles, and explains why constants don't matter at scale.
Operations as n grows
count how work scales, ignore constantsO(1) — same cost at any scale. The dream.
Step 1 of 6. O(1): the same amount of work, always. `arr[0]` takes one operation whether the array has 10 elements or 10 million. The input size does not matter. O(1) means constant time. Hash table lookups, stack push/pop, and array indexing are all O(1).
Step 01 / 06
O(1): the same amount of work, always
arr[0] takes one operation whether the array has 10 elements or 10 million. The input size does not matter.
O(1) means constant time. Hash table lookups, stack push/pop, and array indexing are all O(1).
// O(1) — constant: one operation regardless of nint first = arr[0];// O(n) — linear: visit every element oncefor (int i = 0; i < n; i++) sum += arr[i];// O(n²) — quadratic: nested loop over the same arrayfor (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (arr[i] > arr[j]) count++;// O(log n) — halving the work each stepwhile (lo <= hi) { int mid = lo + (hi - lo) / 2; if (arr[mid] == target) return mid; if (arr[mid] < target) lo = mid + 1; else hi = mid - 1;}// O(n log n) — divide into halves, linear mergemerge_sort(arr, 0, n);