<style> .reveal-up, .reveal-left, .reveal-right, .reveal-stagger > * { opacity: 1 !important; transform: none !important; } </style>
  1. Learn
  2. DSA
  3. Foundations
All lessons in this course

Lesson 2 of 50 14 minBeginner

Measuring speed: Big-O notation

Describe how an algorithm’s work grows with the input size, learn the common growth rates, and the rules for simplifying Big-O.

How do you say one algorithm is faster than another? Seconds are a poor measure: the same code runs at different speeds on a laptop and a server, and the time changes with the input. Instead we count how many basic steps the algorithm does for an input of size n, and we ask one question: as n grows, how fast does the work grow?

Big-O notation is the short way to write that growth. O(n) means “the work grows in proportion to n”: twice the data, about twice the work. O(n²) means twice the data, four times the work.

Interactive

How fast does the work grow?

Turn curves on and off. Drag n to make the input bigger and watch which curves leave the chart.

n = 20600input size n →
Input size
GrowthStepsTime*
O(1)1< 1 µs
O(log n)20< 1 µs
O(n)1,000,00010 ms
O(n log n)19,931,569199 ms
O(n²)1,000,000,000,0002.8 hours
O(2ⁿ)10^301030longer than the universe has existed

*At about 100 million simple steps per second.

At this size the shape of the curve decides everything: O(n log n) finishes in a blink while O(n²) takes hours or years. Faster hardware cannot fix a bad growth rate.

The common growth rates

Big-ONameTypical examplen doubles → work
O(1)ConstantRead array[i], Dictionary lookupstays the same
O(log n)LogarithmicBinary search in sorted data+1 step
O(n)LinearOne loop over the data, finding the max× 2
O(n log n)LinearithmicMerge sort, Array.Sorta bit more than × 2
O(n²)QuadraticComparing every pair (nested loops)× 4
O(n³)CubicThree nested loops, naive matrix multiply× 8
O(2ⁿ)ExponentialTrying every subsetsquared!
O(n!)FactorialTrying every order (permutation)astronomical

Rule 1: drop the constants

Big-O describes the shape of growth, not the exact count. An algorithm that does 3n + 5 steps is O(n). One that loops twice over the data (2n) is still O(n). Constants depend on the machine and compiler; the shape does not.

Constants.cs
// 2n steps → O(n)int min = int.MaxValue, max = int.MinValue;foreach (int x in data) if (x < min) min = x;foreach (int x in data) if (x > max) max = x; // n/2 steps → still O(n)for (int i = 0; i < data.Length / 2; i++)    (data[i], data[^(i + 1)]) = (data[^(i + 1)], data[i]);

Rule 2: keep only the biggest term

If an algorithm does n² + n + 100 steps, the n² part dominates as n grows (for n = 10,000 it is 100,000,000 against 10,100). So it is O(n²).

BiggestTerm.cs
// Part 1: O(n)foreach (var beam in beams) Validate(beam); // Part 2: O(n²): every pairfor (int i = 0; i < beams.Length; i++)    for (int j = i + 1; j < beams.Length; j++)        CheckClash(beams[i], beams[j]); // Total: O(n + n²) = O(n²)

Rule 3: different inputs get different letters

If a method works on two collections of unrelated sizes, do not call both “n”. Looping over a and then over b is O(a + b). Looping over b inside a loop over a is O(a × b).

TwoInputs.cs
// O(a + b)foreach (var c in columns) Draw(c);foreach (var b in beams) Draw(b); // O(a × b)foreach (var c in columns)    foreach (var b in beams)        if (Touches(c, b)) Connect(c, b);

Best, average and worst case

The same algorithm can do different amounts of work for different inputs of the same size. Linear search finds the first element in 1 step (best case), but a missing element needs n steps (worst case). On average it checks about half.

CaseLinear searchUsed for
BestO(1): it is the first elementRarely useful on its own
AverageO(n): about n/2 checksTypical real-world behaviour
WorstO(n): last element or not foundGuarantees: “never slower than this”

When people say “binary search is O(log n)” without saying which case, they mean the worst case. It is a promise: no input of size n will take longer.

Big-O, Big-Ω and Big-Θ

  • Big-O (O): an upper bound. “It grows no faster than this.”
  • Big-Omega (Ω): a lower bound. “It grows at least this fast.” Any comparison sort is Ω(n log n).
  • Big-Theta (Θ): both at once. “It grows exactly like this.” Merge sort is Θ(n log n).

In everyday conversation and interviews, people say Big-O and mean the tight bound. That is fine; just know the other two exist.

Quick check

An algorithm does 5n² + 300n + 2000 steps. What is its Big-O?

Quick check

An O(n²) algorithm takes 2 seconds for 10,000 items. About how long for 20,000 items?

Quick check

Binary search on 1,000,000 sorted items needs about 20 checks. About how many for 2,000,000?

Practice

Give the Big-O of each method below (n = a.Length).

Show a hint

Count loops: one after another add, one inside another multiply, a fixed number of steps is O(1).

Show the solution
Solution.cs
int First(int[] a) => a[0];                       // O(1) int Sum(int[] a)                                   // O(n){    int s = 0;    foreach (int x in a) s += x;    return s;} bool HasPairWithSum(int[] a, int target)           // O(n²){    for (int i = 0; i < a.Length; i++)        for (int j = i + 1; j < a.Length; j++)            if (a[i] + a[j] == target) return true;    return false;} int SumFirstTen(int[] a)                           // O(1): at most 10 steps{    int s = 0;    for (int i = 0; i < Math.Min(10, a.Length); i++) s += a[i];    return s;}

Remember

  • Big-O describes how the work grows with the input size n, not the exact time.
  • Common rates from fast to slow: O(1), O(log n), O(n), O(n log n), O(n²), O(2ⁿ), O(n!).
  • Drop constants, keep the biggest term, and use different letters for different inputs.
  • Unless stated otherwise, Big-O means the worst case.
0 of 50 lessons completed