Big O Notation Examples


Big O Notation Examples

Michael
Okay Hope, let's finally tackle big O notation. It's that tool programmers use to compare how efficient different algorithms are, especially when the input gets really large.

Hope
Right, and it's not about exact seconds — it's about how the number of operations grows as the input size grows. So if you're sorting a million numbers instead of ten, big O tells you which algorithm won't take forever.

Michael
Exactly. And we care about the worst case — that's the upper bound. If an algorithm sometimes finishes quickly but in the worst case takes a huge number of steps, big O captures that worst scenario.

Hope
So where do we start with actually finding it? Let's say I have a simple loop that sums numbers in a list.

Michael
You first count the operations. One operation to initialize a variable, then the loop runs n times — where n is the size of the input — and inside the loop you have two operations. So total operations f(n) = 2n + 1.

Hope
But we don't keep the 2 or the 1, right? We simplify down to just n. That's the big O — O(n).

Michael
Right, but it's worth seeing the math behind that simplification. The formal definition says f(n) = O(g(n)) if there exist constants c and N such that for all n >= N, f(n) <= c * g(n). So for f(n)=2n+1, we can pick c=3 and N=1, and then 2n+1 <= 3n holds.

Hope
So g(n) has to be a function that, when multiplied by some constant, eventually stays above f(n). That's why we pick the simplest g that works — in this case n — and we drop any smaller terms and constants because they become irrelevant for large n.

Michael
Exactly. For something like 2n² + n + 1, the n² term dominates as n grows, so big O is O(n²). You can't use just n because no matter what constant you multiply it by, n² will eventually be bigger.

Hope
Good. So the rule of thumb: keep the largest term, drop constants. If terms are added, the biggest one wins. If they're multiplied, you keep all factors.

Michael
Now let's talk about the common complexity classes. There's a chart you should know: O(1) constant time — a single operation regardless of input — O(log n) logarithmic, O(n) linear, O(n log n), O(n²), O(2ⁿ), and O(n!) — which is the worst.

Hope
Let's go through examples. First is example 0: checking if a number is odd or even. That's just a modulus operation — it's O(1), constant time, because it doesn't matter how big the number is.

Michael
Example 1: summing all numbers in a list — that's a loop over n elements, one operation per element, so O(n).

Hope
Example 2 has a nested loop. The outer loop runs n times, and for each iteration the inner loop runs n times. That's n * n = n² operations, so O(n²).

Michael
Example 3 introduces two separate inputs. You have a loop over list one and a loop over list two, but they're not nested. So total operations are n + m, and big O is O(n + m). You can't simplify further because n and m are independent.

Hope
Example 4: a nested list. If you have h outer lists and each inner list has w elements, and you loop through both, the total is h * w operations. That's O(h * w).

Michael
Example 5 is the Fibonacci sequence with recursion. Each call makes two more calls, and the recursion depth is n, so the tree has 2ⁿ nodes — that's O(2ⁿ). Terrible for performance.

Hope
Example 6 shows why you have to be careful with built-in functions. Finding the max of a list is O(n) because it has to check every element. So if you do that plus a loop over another list, you get O(n + m).

Michael
Example 7 is recursion that divides n by 2 each time. With one recursive call and the problem size halving, the number of steps is log₂(n). So O(log n).

Hope
Example 8 deals with a list of strings. The outer loop runs n times for each string, but inside you also loop through characters of each string — length k — and sometimes sort. Sorting is O(k log k). So total is O(n * k log k).

Michael
Example 9 is more complex: sorting two dictionaries, then a while loop that pops from the front — which is O(size of list) — and reinserts strings. The longest string length k multiplies the work, so final complexity is O(n log n + m log m + (n+m)*k).

Hope
Example 11 has three nested loops, each running n times — that's O(n³). And example 12 is factorial recursion: for input n, the function calls itself n times, each of those calls n-1 times, and so on. That's n! — O(n!).

Michael
So if you understand the first eight examples, you're in great shape. The later ones show how to handle more realistic code, but the core idea is always: count operations, drop constants, keep the dominant term, and use different variables for different inputs.

Hope
And that gives you the big O — a simple way to talk about how your algorithm will behave when the input gets huge. It's a superpower for writing efficient code.

Ask anything
We've read this whole study set, so questions about it get the best answers.