Big O Notation
In one sentence: Big O notation is a way to say that one function never grows faster than another, once the input is big enough.
Read it out loud: “f of n is big O of g of n, if and only if, there exists a positive constant c and a starting point n sub 0, such that for every whole number n that is at least n sub 0, zero is less than or equal to f of n, and f of n is less than or equal to c times g of n. Here f of n is a count of steps, n is the size of the input, and g of n is a simpler reference function such as n.”
Before you start
The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.
- Functions and the input size
A function is a rule that takes an input and gives back one number. Here is the number of basic steps an algorithm takes when its input has size . The input size is a count of items, such as 10 names in a list or 1000 photos. The notation is read ‘f of n’, meaning f applied to n.
- Powers, like and
A small raised number is an exponent, and it says how many times to multiply a number by itself. So , read n squared, and , read n cubed. For , . For , , which is a thousand times bigger than itself.
- The logarithm
A logarithm answers the question: how many times can I divide by a number before I reach 1? Here means base 2, so it counts how many times can be cut in half before reaching 1. For example , because 8 halved three times gives 4, then 2, then 1. Big O does not care about the base, because changing the base only multiplies by a fixed constant.
- The signs , and
means ‘there exists’, which says that at least one choice works. means ‘if and only if’: the statement on the left is true exactly when the statement on the right is true, in both directions. means ‘less than or equal to’, and means ‘greater than or equal to’. So is true, because the two sides are equal.
- Steps of an algorithm
An algorithm is a recipe a computer follows, one small step at a time, such as comparing two numbers, adding two numbers or reading one item. Counting steps is the usual way to measure running time, because the same number of steps takes about the same time on a given machine. Big O counts how the number of steps grows, not how many seconds pass.
- Constants and
A constant is a fixed number that does not change as grows. The letter is a constant multiplier that scales up. The letter is a starting point, a fixed input size after which the rule must hold. A bigger or a bigger only makes the rule easier to satisfy, so the definition asks only that some choice exists.
What each symbol means
| Symbol | Meaning |
|---|---|
| The function you want to measure, such as the number of basic steps an algorithm takes on an input of size . It counts steps, so it has no unit of its own. For example, an algorithm that does steps on 10 items takes steps. | |
| The simpler reference function you compare against, such as , or . It is also a count, and it is chosen to be as simple as possible so the shape of growth is easy to read. When , the comparison is with a straight line, where doubling the input doubles the reference. | |
| Read ‘big O of g of n’. It is the set of all functions that stay below a fixed multiple of once is large enough. It is a set of functions, so it has no unit. Saying that is in means stays under some fixed multiple of for large . | |
| The size of the input, a whole number that counts items: how many names are in a list, or how many pixels are in a photo. It has no unit, it is never negative, and it can grow without limit. Typical textbook values are 10, 100 or 1000. | |
| A positive constant, a fixed multiplier such as 4 that scales up so that it sits above . It has no unit and does not depend on . Once a value of is chosen, it stays the same for every input size. Any bigger value that works is also allowed. | |
| Read ‘n sub zero’. It is a starting point, a fixed whole number such as 5. The inequality only has to hold for each at or beyond , so small inputs with odd behavior are ignored. It has no unit. | |
| Read ‘if and only if’. It joins two claims that say the same thing in both directions. The left statement and the right statement are both true, or both false. It has no unit and no numeric value. | |
| Read ‘there exists’. It says that at least one choice of and makes the rule true. It does not say how many choices work, only that one does. It has no unit and no numeric value. |
The formula piece by piece
The input size, the one number that the step count depends on. As grows from 10 to 100 to 1000, the work of a growing algorithm grows along with it, and that growth is what big O describes.
The simple reference shape, such as , which is a straight line, or , which is a curve that bends up faster. It is the yardstick that is compared against, and it is chosen to be as simple as possible.
The yardstick scaled up by the constant . Multiplying by can lift the yardstick above , so a factor of 3 or 100 in front of does not change the shape. Any positive that works is allowed.
The sandwich: is at least 0, and it is at most the scaled yardstick. Both parts must hold for every that is large enough. The left part is easy to satisfy, because a step count is never negative.
In one plain sentence: the function is big O of exactly when there is some positive constant and some starting point , so that for every input at or beyond , is at least 0 and at most times .
What the formula is doing, and why
What goes in and what comes out. Big O notation takes a step-counting function and a simpler reference . It answers one question: does stay below some fixed multiple of as the input gets big? The answer is yes or no, and we write a yes as . The notation describes the shape of growth, not an exact number of steps. The letter O stands for ‘order’, as in the order of growth.
Why the shape, not the exact count? An algorithm that takes steps and one that takes steps both grow in a straight line as grows. Both are . The exact numbers depend on the machine, the language and the details of the code, but the shape stays the same. Big O keeps the part that stays the same across machines.
Why drop constants and small terms? The constant absorbs any fixed multiplier, so and both count as . Smaller terms are dropped for the same reason. For large , the biggest term decides almost everything. Take . At , the term is 100,000,000, while is only 10,000,000. The term is 10 times bigger, so the class is .
Why the starting point and the bound? The starting point lets you ignore small inputs, where the exact behavior can look different. Together, the constant and the starting point say that past some point, never climbs above a fixed multiple of . This is an upper bound. It limits how fast can grow, and it says nothing about how fast must grow.
Why it helps you choose algorithms. An method beats an method on large inputs, even when the method has a smaller constant. Constants and small inputs still matter in practice, so big O is the first comparison, not the last. Big O says nothing about tiny inputs, where a simple method can be faster.
An everyday picture
Think of making sandwiches for a party. Suppose each sandwich takes about 3 minutes of work, plus a 5-minute setup at the start. Twice as many guests then takes about twice as much work, so the time forms a straight line. That line is the shape, and the job is , where is the number of guests. Whether each sandwich takes 3 minutes or 4, and whether the setup takes 5 minutes or 10, the line has the same shape. Big O asks only about the shape, not the exact count.
Worked example: Showing 3n + 5 is O(n)
Let be the step count, and let be the reference. Here is the input size, a count of items such as the number of names in a list. We must find a constant and a starting point so that for every .
- Pick a constant. We try , which is a number bigger than the 3 in front of . Any constant bigger than 3 can work for large , because the matters less and less as grows. So 4 is a convenient choice.
- Turn the rule into a simple question. We need . Subtract from both sides, which keeps the inequality true, and we get . So the rule holds exactly when is at least 5.
- Choose the starting point. The starting point works, because for every the condition is true. Here is why a starting point is needed: at , the left side is , while the right side is , so the rule fails for small inputs.
- Check the boundary. At , the left side is and the right side is . The two sides are equal. The definition allows this, because it uses , less than or equal to, and not a strict less than.
- Check a larger input. At , the left side is and the right side is , so . At the left side is 305 and the right side is 400. The gap grows as the input grows, which matches the rule.
- Watch the ratio. Divide the step count by . At , the ratio is . As grows, settles toward 3, because the becomes less important compared with .
- Check the lower part. The left part, , holds for every , because 3 times a count is never negative and adding 5 keeps the result positive. So both parts of the definition are met for .
Answer: With and , the definition holds, so is O(n). The and the factor 3 do not change the shape of the growth, which is a straight line in .
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Thinking means exactly steps. Big O is an upper bound on growth, not an exact count. Both steps and steps are .
- Keeping constants and lower terms in the answer. Write , not , and write , not .
- Judging small inputs by big O. At , an method with a tiny constant can beat an method with a large one. Big O is about what happens when is large.
- Forgetting to name the case being measured. Big O can describe the best, average or worst case of an algorithm, so say which one, such as worst-case .
Where you will meet it
- Comparing sorting methods, such as merge sort at and bubble sort at in the worst case.
- Choosing data structures. A hash table lookup takes time on average, which means a fixed amount of work however big the table is. A search through a plain list takes time, which grows with the length of the list.
- Database indexes, where a B-tree lookup takes time. A B-tree is a tree-shaped index that lets a database find a row quickly, and its work grows slowly as the table gets bigger.
- Analyzing loops and recursion in textbooks and programming interviews.
Questions people ask
Why are constant factors ignored?
The constant absorbs them. A faster computer changes the size of each step, which changes the constant but not the shape of the growth. Big O compares how methods scale as inputs grow, and constant factors depend on the machine, so they are left out.
What is the difference between O, Omega and Theta?
Big O is an upper bound: grows no faster than . Omega, written , is a lower bound: grows at least as fast as . Theta, written , is a tight bound: is both and of the same , so it grows at the same rate, up to constant factors.
Is an $O(n)$ method always faster than an $O(n^{2})$ method?
Not for small inputs. At , an method with a small constant can finish first. For large the method wins, because its growth is much slower.
Related formulas
- Master Theorem: The master theorem tells you how long a program takes when it cuts a problem into smaller pieces, solves each piece and joins the answers.
- Sum of the First n Integers: This formula gives the total of the numbers 1, 2, 3 and so on up to in one quick calculation, with no adding one number at a time.
- Geometric Series: A geometric series adds up a list where each term is the one before it times a fixed number, and this formula finds the whole sum in one step.
- Binary Search Steps: Binary search finds an item in a sorted list by checking the middle and throwing away half, and this formula counts how many checks that takes at most.