Big-O, Big-Omega & Big-Theta
Describe an algorithm's growth rate with the correct asymptotic notation.
By Invicly Tech, , in Data Structures & Algorithms. About 10 minutes to read.
In short
Asymptotic notation describes how an algorithm's cost grows as the input size n grows, ignoring machine speed and constant factors. Big-O gives an upper bound, Big-Omega a lower bound and Big-Theta a tight bound. It lets you compare algorithms before writing a single benchmark.
Think of it like this: Comparing walking and driving by how travel time grows with distance, not by who has the faster shoes.
Explained simply
When you write a program, you want to know: will it still be fast when the input gets big? Big-O notation is a short way to answer that. It does not tell you seconds. It tells you how the amount of work grows as the input grows.
Imagine washing plates. If washing one plate takes one minute, then 10 plates take 10 minutes and 100 plates take 100 minutes. The work grows in a straight line with the number of plates. We call that O(n), where n is the number of plates.
Now imagine you must compare every plate with every other plate to find matching pairs. With 10 plates that is about 100 comparisons. With 100 plates it is about 10,000. That is O(n^2), and it gets slow very fast. Big-O helps you spot this before your program meets real data.
There are three related words. Big-O is a ceiling: the work grows no faster than this. Big-Omega is a floor: the work grows at least this fast. Big-Theta means both at once, a tight fit. In everyday talk, people say Big-O and usually mean the tight answer.
Press Play on a diagram to walk through it one step at a time.
- 1O(1)
Array index, hash lookup.
- 2O(log n)
Binary search.
- 3O(n)
Single pass over the input.
- 4O(n log n)
Merge sort, heap sort.
- 5O(n^2)
Nested loops over pairs.
- 6O(2^n)
All subsets.
- Upper bound
- Grows no faster than
- Most quoted in practice
- Lower bound
- Grows at least as fast as
- Used for lower-bound proofs
- Tight bound
- Both O and Omega hold
- Most precise statement
- 1Write the claim
3n + 5 <= c * n for large n
- 2Pick c = 4
Now need 3n + 5 <= 4n
- 3Solve
5 <= n, so n0 = 5
- 4Check n = 10
35 <= 40, holds
- 5Conclude
3n + 5 is O(n)
- O(1): 1 step
- O(log n): about 10 steps
- O(n): 1,000 steps
- O(n log n): about 10,000 steps
- O(n^2): 1,000,000 steps
- Both run in well under a second
- O(2^n): a 302-digit number
- O(n!): even bigger
- Would not finish before the sun burns out
In depth
What "upper bound" really means
Saying f(n) is O(g(n)) means: after some starting size n0, f(n) is never more than c times g(n), for some fixed number c. You get to choose c and n0.
Take f(n) = 3n + 5. Pick c = 4. Is 3n + 5 at most 4n? That is true when n is 5 or more. So with c = 4 and n0 = 5, we have shown 3n + 5 is O(n). Small inputs before n0 do not matter, because Big-O only cares about large n.
- You choose the constant c and the starting point n0
- Only behaviour for large n counts
- A formal proof is just finding one valid c and n0
Why constants and small terms are dropped
Constants depend on the machine, the language and the compiler. A loop doing 2 steps per item on a slow laptop might beat a loop doing 1 step per item on a fast server. Big-O ignores this so you can compare ideas, not hardware.
Smaller terms vanish for the same reason. In n^2 + 1000n, when n is one million, n^2 is a thousand times larger than 1000n. The biggest term always wins in the end, so it is the only one we keep.
Best, average and worst case are different questions
Case and bound are two separate ideas. The case picks which input you are talking about. The bound describes how that cost grows.
Linear search finds the target first try in the best case, which is Theta(1). In the worst case it checks all n items, which is Theta(n). On average, over random positions, it checks about n/2 items, still Theta(n). Interviews and real systems usually care about the worst case, because that is what users feel on a bad day.
- Target is the first item
- 1 check
- Theta(1)
- Target at a random spot
- About n/2 checks
- Theta(n)
- Target last or missing
- n checks
- Theta(n)
Turning Big-O into a feeling for speed
A simple rule: plain Python does roughly 10 to 50 million simple steps per second. Plug your input size into the growth rate to guess the running time.
With n = 100,000, an O(n log n) solution does about 1.7 million steps and finishes in well under a second. An O(n^2) solution does 10 billion steps and takes minutes. That one calculation tells you whether to start coding or look for a better idea.
- n up to about 10: O(n!) may be fine
- n up to about 20: O(2^n) is fine
- n up to about 5,000: O(n^2) is fine
- n up to about 10^6: aim for O(n log n) or O(n)
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Define upper, lower and tight bounds~45m
- 2Drop constants and lower-order terms~45m
- 3Rank common growth rates~45m
- 4Separate best, average and worst case~45m
Key points
- Drop constants and lower-order terms: 3n^2 + 5n + 7 is O(n^2).
- O(n log n) is the best possible for comparison-based sorting.
- Best, average and worst case are separate questions from O, Omega and Theta.
- For n = 10^6, O(n log n) is about 2 x 10^7 steps while O(n^2) is 10^12.
Key terms
Tap a card to flip it. 0/4 known
Examples
def contains(xs, target): # O(n) time, O(1) space
for x in xs:
if x == target:
return True
return False
def has_pair_sum(xs, k): # O(n^2) time
for i in range(len(xs)):
for j in range(i + 1, len(xs)):
if xs[i] + xs[j] == k:
return True
return FalseOne loop is linear; a loop over all pairs is quadratic.
Counting the steps of a small function
The function below has one loop over the list and one nested loop over all pairs. We count steps for n = 4. The first loop runs 4 times. The pair loop runs 3 + 2 + 1 + 0 = 6 times. In general that is n + n(n-1)/2 steps. Drop the constant 1/2 and the smaller n term, and you get O(n^2). The pair loop decides everything.
def steps(xs):
count = 0
for x in xs: # n steps
count += 1
for i in range(len(xs)): # n(n-1)/2 steps
for j in range(i + 1, len(xs)):
count += 1
return count
for n in (4, 100, 1000):
print(n, steps(list(range(n))))
# 4 10
# 100 5050
# 1000 500500 -> grows like n^2 / 2Best, average and worst case of linear search
Search for a value in [7, 3, 9, 1, 5]. Looking for 7: found at index 0 after 1 check (best case). Looking for 5: found after 5 checks. Looking for 8: not there, so all 5 items are checked (worst case). If the target is equally likely to be anywhere, the average is (1+2+3+4+5)/5 = 3 checks, about n/2. Both average and worst are O(n); only the best case is O(1).
def linear_search(xs, target):
checks = 0
for i, x in enumerate(xs):
checks += 1
if x == target:
return i, checks
return -1, checks
data = [7, 3, 9, 1, 5]
for t in (7, 5, 8):
print(t, linear_search(data, t))
# 7 (0, 1) 5 (4, 5) 8 (-1, 5)Quick reference
- Order: O(1) < O(log n) < O(n) < O(n log n) < O(n^2) < O(2^n) < O(n!)
- Keep the biggest term, drop constants: 5n^2 + 3n + 9 is O(n^2)
- O = ceiling, Omega = floor, Theta = both (tight)
- Case (best, average, worst) is a separate question from the bound
- Python does roughly 10^7 to 10^8 simple steps per second
- n = 10^5 means you need about O(n log n) or better
- log2(1,000,000) is about 20
Common mistakes
- Treating Big-O as meaning worst case; it is just an upper bound on a function.
- Ignoring constants entirely when n is small and a simpler algorithm is faster.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write largest(xs) that returns the biggest number in a non-empty list without using max(). Example: [4, 9, 2] returns 9. State its time complexity.
- Exercise 2medium
has_duplicate(xs) returns True if any value appears twice. The obvious version compares all pairs in O(n^2). Write an O(n) version. Example: [3, 1, 4, 1] returns True, [1, 2, 3] returns False.
- Exercise 3hard
Given a sorted list and a number k, count pairs (i < j) with xs[i] + xs[j] <= k. Checking all pairs is O(n^2); do it in O(n). Example: [1, 2, 3, 4], k = 5 gives 4 pairs: (1,2), (1,3), (1,4), (2,3).
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Does Big-O tell me how many seconds my code takes?
- No. It tells you how the work grows as input grows. To get seconds, combine it with a rough speed like 10 to 50 million simple Python steps per second, or measure with timeit.
- Why do people say Big-O when they mean Big-Theta?
- Habit. In interviews and daily work, "this is O(n)" usually means "this is exactly linear". In proofs and textbooks, the difference matters.
- Is O(2n) different from O(n)?
- No, they are the same class because constants are dropped. Two passes over the data is still linear.
- Can an O(n^2) algorithm beat an O(n log n) one?
- Yes, for small inputs. Insertion sort beats merge sort on about 20 items, which is why real sort functions use it for small pieces.