Short answer
Count the steps that grow with the input, drop constant factors and smaller terms, and name the remaining class. A single loop over n items does about n steps, which is O(n). A nested pair of loops over n does about n squared steps, which is O(n squared). Big O is an upper bound on that growth, not a timer reading and not a promise about a particular computer.
Last verified against the sources listed below.
Key takeaways
- Wikipedia says big O in computing classifies algorithms by how run time or space grows with the input, and it is an upper bound.
- Drop constants and lower-order terms. Stanford's lecture reduces 5n + 2 to O(n).
- One loop that adds a pass per extra item is O(n). Two nested loops over n are O(n squared).
- A triangle of pairs, n(n-1)/2 steps, is still O(n squared) after the constant factor is dropped.
- Common classes include O(1), O(log n), O(n), O(n log n), and O(n squared), each with a standard example.
How to calculate big o notation is a cleanup step you do after you count how the work grows. Wikipedia says that in computer science the notation classifies algorithms by how running time or space grows with the input, and that a big O description is only an upper bound. You keep the fastest-growing term. You drop constants and lower-order terms.
The letter O comes from Ordnung, order of approximation. Bachmann proposed the notation in 1894, and Landau extended it in 1909.
For algorithms, the useful reading is the one Wikipedia gives for a step count: if T(n) is O(n squared), the algorithm has at most on the order of n squared time complexity. If you are writing that argument up, the Word Counter will count the words in the paragraph.
It will not choose the class for you.
What do you drop from the count?
You drop constant factors and any term that grows more slowly than the largest one. Wikipedia’s simplification rules say that in a sum you keep the term with the largest growth rate, and in a product you omit factors that do not depend on the input. Multiplying by a nonzero constant does not change the class: O of a constant times g is O(g).
Stanford’s CS106B lecture puts the same trick in one line. 5n + 2 is O(n). The lecture’s reason is that for a very large n, the constant 5 and the added 2 stop mattering beside functions that grow faster. Its practice case 20n^3 + 10n log n + 5 strips to n^3 + n log n, then to O(n cubed), because the cubic term dominates.
Wikipedia uses a polynomial to show the algebra. 6x^4 - 2x^3 + 5 is O(x to the fourth). The page also says O(x to the tenth) would be a true but sloppier upper bound, while O(x cubed) is false for that polynomial. When you calculate a class, you want the simple bound that is still honest, not the loosest label in the table.
How many steps is a single loop?
A single loop over n items is O(n), because the body runs once per item. Take this snippet and a concrete n of 4.
let steps = 0;
for (let i = 0; i < n; i++) {
steps = steps + 1;
}
When n is 4, the body runs 4 times, so steps ends at 4. That is one step per item.
If the body did three assignments instead of one, the count would be 3n, which is 12 when n is 4. Wikipedia says a constant factor is omitted, and Stanford reduces 5n + 2 to O(n).
Both n and 3n are O(n).
Stanford’s vectorMax example is the same pattern in a course lecture. The function walks a vector once, and each extra element adds one more iteration.
The lecture ignores the setup assignments and the return, and it calls the relationship linear, O(n). Wikipedia’s table gives the everyday example: finding an item in an unsorted list is linear.
The loop above is that scan, with the step count written out.
How many steps is a nested pair?
A nested pair of loops that each run n times is O(n squared). Count the inner body, not the look of the braces.
let steps = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
steps = steps + 1;
}
}
When n is 4, the inner statement runs 4 times 4, which is 16. Stanford’s nestedLoop1 is this shape: the inner loop is O(n), and it happens n times, so result is incremented n squared times. The lecture calls that quadratic. Princeton’s COS 226 lecture table lists a double loop, described as checking all pairs, under quadratic growth.
Now add a setup cost of 2 assignments before the loops. For n of 4, the total is 2 + 16, which is 18.
The expression is 2 + n^2. Wikipedia’s sum rule keeps the fastest term and omits the rest, so this is O(n squared).
The added 2 does not create a new class.
A tighter pair of loops still lands in the same class. An Illinois lecture note on nested loops counts an inner loop that runs only from i + 1 to n.
The body then executes n(n - 1) / 2 times. For n of 4, that is 4 * 3 / 2, which is 6, not 16.
The note says this is still O(n squared): the change improved the constant factor, not the big O class. Wikipedia agrees, because dividing by 2 is multiplication by a constant.
| n | Single loop steps | Full nest (n squared) | Triangle n(n-1)/2 |
|---|---|---|---|
| 4 | 4 | 16 | 6 |
| 10 | 10 | 100 | 45 |
| 20 | 20 | 400 | 190 |
The triangle column is about half of the square column, and it pulls away from the linear column as n grows. Both nested counts stay O(n squared). Neither column is a benchmark. They are step counts from the loops.
Which class is which?
Name the class only after the algebra is done. Wikipedia lists these as common orders for running time, with the slower-growing functions first. Each row uses one example from that table or from the Stanford lecture.
| Class | Name in the sources | One plain example |
|---|---|---|
| O(1) | Constant | Median of a sorted array, at a known index. Stanford also lists adding or removing at the end of a vector. |
| O(log n) | Logarithmic | Binary search in a sorted array. Stanford’s number-guessing version cuts the range in half each guess. |
| O(n) | Linear | Finding an item in an unsorted list, or one loop over n items. |
| O(n log n) | Linearithmic, or n log n | Merge sort or heapsort. Wikipedia also lists the fastest comparison sort here. |
| O(n squared) | Quadratic | A nested pair of loops over n, or simple sorts such as bubble sort, selection sort, and insertion sort. |
Stanford’s guessing game is a worked logarithm, not a timer. With numbers from 1 to 128, the range falls 128, 64, 32, 16, 8, 4, 2, 1.
That is 8 guesses, which the lecture writes as log2(128) + 1. Doubling the range to 256 adds one guess.
Wikipedia treats a change of log base as the same big O class, so this is O(log n).
O(1) means the step count does not grow with the number of elements. Stanford’s examples are operations at the end of a well-built vector, and later in that course, stack push and pop. It is not a compliment you attach to a loop that still walks every item.
Where do people mis-label a loop?
The usual miss is keeping a constant, or ignoring a loop that hides inside a function call. 3n + 4 is not a separate class from n.
Stanford’s lecture tells you to throw out the constants and keep the biggest factor. Writing O(3n) states a true upper bound and then fails to simplify it.
Writing O(n) is the calculation.
The other miss is a loop you did not type. Stanford shows a function that inserts at the front of a vector inside a loop of n passes. Each insert shifts the current elements, so the lecture’s answer is O(n squared), not O(n). When you calculate a class from real code, count the work inside the called operation, not only the for lines on the screen.
Worst case is the number to publish when the docs disagree with a lucky early exit. Stanford’s linear search is O(1) if the target is first and O(n) if it is last. The lecture reports the worst case. Wikipedia’s upper-bound definition still holds: O(n) covers that scan, and O(1) does not cover the last-element case.
Big O does not say which program feels faster on your laptop. It says which term survives as n gets large.
A single counted loop survives as O(n). A nested pair survives as O(n squared).
If you want a second pair of eyes on the write-up, count the words, then check that every constant you dropped really was a constant. Python and other languages use the same classes.
The programming paradigm behind the code does not change the algebra of the loops.
Frequently asked questions
Is big O the same thing as the exact number of steps?
No. Wikipedia says a big O description only gives an upper bound on the growth rate, and that writers omit constant factors and lower-order terms so the comparison function stays simple. The same page shows that a degree-4 polynomial is O(x to the fourth), and that calling it O(x to the tenth) is also true and less precise. The calculation you want is the simple class left after you drop constants and smaller terms, not every assignment in the function.
Do logarithms with different bases count as different classes?
Wikipedia says logs with different constant bases are equivalent with respect to big O notation. A change of base is multiplication by a constant, and multiplication by a nonzero constant does not change the class. Exponentials do not work that way. The same page says 2 to the n and 3 to the n are not the same order.
Why can a search be O(1) in one case and O(n) in another?
Stanford's linear search lecture says it depends on where the value sits. If the target is the first element, the loop can stop after one comparison, which the lecture calls O(1) for that best case. If the target is last, or missing, the loop walks the whole vector, which is O(n). The lecture's rule for reporting big O on code is to take the worst case, because you do not know the data.
Does a faster computer change the big O class?
The class describes how the step count grows with n, not how many milliseconds a laptop needs. Wikipedia ties T(n) = O(n squared) to an algorithm that has at most the order of n squared time complexity. A faster machine can shrink the constant hidden inside that bound. It does not turn a nested pair of loops into a single loop. This page does not report benchmark times, because a clock reading is a different measurement from the algebra of the loops.
What does Stanford do with 20n cubed plus 10n log n plus 5?
The CS106B lecture strips the constants first, leaving n cubed plus n log n, then keeps the biggest factor, which is n cubed. The answer on the slide is O(n cubed). A second practice line, 2000 log n plus 7n log n plus 5, strips to log n plus n log n and keeps n log n. The lecture also says that if two factors from its table appear, you leave only the one farther to the right, where the faster-growing functions sit.
Sources
- Big O notation · Wikipedia, 2026
- Lecture 7: Big O and Asymptotic Analysis · Stanford University, 2025
- Building Blocks, Chapter 15 (nested loops) · University of Illinois, 2026
- Analysis of Algorithms lecture table · Princeton University, 2026