Big O notation explained: time complexity in plain English, from O(1) to O(n²)
Big O notation describes how the work an algorithm does grows as its input grows. It ignores the exact seconds and keeps only the shape of the growth: O(1) stays flat whatever the size, O(log n) grows very slowly because each step halves the problem, O(n) grows in step with the input, O(n log n) is the usual cost of a good sort, and O(n²) explodes because every item meets every other item. Knowing the class tells you, before you run anything, whether your code will survive ten times more data.
Video · What is Big O notation? 5 classes in under a minute · 0:52
Big O notation in one paragraph
Prefer to watch? The one-minute Short shows the core idea with diagrams; this guide goes further.
You write a function, test it on ten items, and it is instant. A month later it runs on a million items and the page times out. Nothing changed in the code. What changed is the input size, and the code's work grew much faster than you expected. Big O notation is the tool for predicting that growth before it bites.
Big O describes how the work an algorithm does grows as its input grows. It does not measure seconds, which depend on the machine. It measures the shape of the growth: flat, slow, steady, or explosive.
Throughout this guide we use one everyday setting: a party. Guests arrive, coats get checked, names go on a list, people shake hands. Every common complexity class shows up somewhere in that party.
What Big O notation measures
We call the size of the input n: the number of guests, rows, items or characters. Big O answers one question: if n gets ten times bigger, how much more work do I do?
The formal definition, as the NIST Dictionary of Algorithms and Data Structures gives it, says a function f(n) is O(g(n)) when, past some point, f(n) never grows bigger than a constant times g(n). In plain words, g is a ceiling on the growth. An algorithm that is O(n²) never grows faster than n² once n is large enough.
That word "ceiling" matters. Strictly, Big O is an upper bound. When people want to say "grows exactly like this", the precise term is Big Theta (Θ). In everyday engineering talk, people say Big O and mean the tight answer, and that is how this guide uses it too.
Why we drop constants and smaller terms
Say an algorithm does n² + 3n + 4 steps. At n = 10 that is 134 steps, and the n² part is already 100 of them. At n = 1,000 it is 1,003,004 steps, and the n² part is 1,000,000 of them. The rest is noise. NIST uses this exact example: n² + 3n + 4 is O(n²).
The same logic drops constant factors. An algorithm that does 5n steps and one that does n steps are both O(n): double the input and both double their work. Big O is about the slope of growth, not the starting height.
This does not mean constants never matter. On small inputs, a simple O(n²) method can beat a clever O(n log n) one. Big O tells you who wins as n grows, which is usually the question that decides whether a system survives.
Best case, worst case and average case
The same algorithm can do very different amounts of work on different inputs of the same size. Looking for a guest on an unsorted list takes one look if they are first, and n looks if they are last or missing.
So we usually state the worst case: the most work for any input of size n. It is the safe promise. Sometimes the average case matters more, as with hash tables, which are constant time on average but can degrade when many keys collide. When someone says "this is O(n)" without saying which case, they almost always mean the worst case.
The common time complexity classes
Here are the classes you meet most often, from fastest to slowest growing, with the party example for each.
| Class | Name | Party example | Double the guests and the work… |
|---|---|---|---|
| O(1) | constant | the coat check finds your coat from your ticket | stays the same |
| O(log n) | logarithmic | finding a name in a sorted guest list by halving | grows by one step |
| O(n) | linear | checking every name badge to find one person | doubles |
| O(n log n) | linearithmic | sorting the guest list with a good method | a bit more than doubles |
| O(n²) | quadratic | every guest shakes hands with every other guest | quadruples |
O(1): constant time
You hand over ticket 42 and the attendant walks straight to hook 42. Ten coats or ten thousand, it is one step. That is O(1), constant time.
In software, reading an item from an array by its position is O(1). Looking up a key in a hash table is O(1) on average: a hash function turns the key straight into a position, so the size of the table does not matter. NIST's entry on hash tables adds the honest caveat that this depends on the hash function and the table being big enough.
O(log n): logarithmic time
Now find "Morgan" on a sorted guest list. Open it in the middle. If the middle name comes after Morgan, throw away the second half; if before, throw away the first. Repeat on the half that is left.
Each look halves what remains. That is binary search, and its work is O(log n). Halving is powerful: a list of 1,000 names takes about 10 looks, and a list of a million takes about 20. Doubling the list adds one more look.
Logarithmic time is why indexes in databases are so valuable: they keep data in an order that lets the search throw away most of it at every step. The same "narrow it down level by level" idea shows up in the way DNS finds an address, from the root to the domain, instead of scanning one giant list.
O(n): linear time
No list at all? Then you walk around and check every badge until you find Morgan. Twice the guests, twice the walking. That is O(n), linear time.
Any job that has to touch every item once is at least O(n): adding up a column, finding the largest value in an unsorted list, copying a file. Linear time is usually fine. It is the class you get when the work is honest and unavoidable.
It also hides in places you might not expect. If a server checks in with every connected client on a timer, as in short polling, the work per round grows linearly with the number of clients.
O(n log n): the cost of a good sort
To print a neat, sorted guest list, you can split the names into two halves, sort each half, and merge the two sorted halves by repeatedly taking the smaller front name. Applied again and again down to single names, that is merge sort, and NIST gives its run time as Θ(n log n).
Where does n log n come from? Halving the list again and again gives about log n levels, and each level does about n work to merge everything. n work, log n times.
O(n log n) is the usual price of sorting well. A million names need roughly 20 million steps, far more than a single pass, but far less than the alternative below.
O(n²): quadratic time
At the end of the evening, the host says everyone must shake hands with everyone. With 10 guests that is 45 handshakes. With 100 guests it is 4,950. With 1,000 guests it is 499,500. Ten times the guests, about a hundred times the handshakes.
That is O(n²), quadratic time. It appears whenever you compare every item with every other item: finding duplicates by comparing all pairs, or a simple sort like insertion sort, which NIST lists as O(n²).
Quadratic code is the most common cause of "it worked in testing." At 100 items it is instant. At 100,000 items it is ten billion comparisons. Often the fix is a different idea: sort first (n log n) and compare neighbours, or remember what you have seen in a hash table (n).
Beyond n²: exponential and factorial time
Some problems grow even faster. Trying every possible subset of n guests (who sits at the top table?) is O(2ⁿ): each extra guest doubles the options, and 30 guests already give over a billion subsets. Trying every possible order (every seating arrangement in a row) is O(n!), which is worse still.
Code in these classes only works for very small inputs. In practice, people use shortcuts, limits or "good enough" answers instead.
How fast each class grows
The table makes the difference concrete. These are rough step counts; logs are base 2 and rounded.
| n | O(1) | O(log n) | O(n) | O(n log n) | O(n²) |
|---|---|---|---|---|---|
| 10 | 1 | about 3 | 10 | about 33 | 100 |
| 1,000 | 1 | about 10 | 1,000 | about 10,000 | 1,000,000 |
| 1,000,000 | 1 | about 20 | 1,000,000 | about 20 million | 1 trillion |
At a million items, the gap between O(log n) and O(n²) is the gap between 20 steps and a trillion.
How to estimate Big O without any math
You rarely need a proof. These rules, said in words, get you most of the way:
- One pass over the data is O(n).
- A pass inside a pass over the same data is O(n²).
- Halving the problem each step gives O(log n).
- Steps one after another add up, and the biggest one wins: a sort followed by a single pass is O(n log n).
- Look inside library calls. "Check if this list contains X" is itself O(n) on a plain list, so doing it inside a loop is O(n²).
Space complexity: the same idea for memory
Big O also describes memory. An algorithm that keeps a copy of the input uses O(n) extra space; one that keeps a few counters uses O(1). The two often trade off: the coat check is O(1) per lookup only because it spends space on numbered hooks and tickets.
Where Big O meets real systems
Big O is a model, not a stopwatch. Two O(n) methods can differ by ten times in speed, and memory layout and caching can matter as much as the class. So use Big O to rule out designs that cannot scale, then measure the ones that remain.
The same habit of asking "what grows with what?" helps outside classic algorithms too. With a language model, for example, every request reads the whole conversation so far, so the input grows with every turn. Growth there costs accuracy as well as time, as the guide on why a bigger context window is not automatically better explains.
The measuring part is exactly what load and performance testing is for: run the system at 10, 100 and 1,000 times today's data, and see whether the curve looks like the class you expected.
Recap
- Big O describes how work grows with input size, not how many seconds it takes.
- Drop constants and smaller terms; keep the part that dominates as n grows.
- O(1) coat check, O(log n) halving a sorted list, O(n) every badge, O(n log n) a good sort, O(n²) every handshake.
- Watch for a pass inside a pass: it is the most common reason code that was fast in testing becomes slow in production.
More guides on software engineering fundamentals are in the article library.
Sources: NIST DADS, big-O notation · NIST DADS, hash table · NIST DADS, binary search · NIST DADS, merge sort · NIST DADS, insertion sort
FAQ
What is Big O notation in simple terms?
Big O notation is a way to describe how the amount of work an algorithm does grows as its input gets bigger. Instead of saying a task takes 2 seconds, it says the work grows like n, or like n squared, where n is the size of the input. That tells you how the code will behave at ten or a thousand times the data.
What does O(n) mean?
O(n), linear time, means the work grows in step with the input. Twice the items means roughly twice the work. Looking at every item once, such as checking every name on an unsorted list to find one person, is O(n).
What does O(log n) mean?
O(log n), logarithmic time, means each step cuts the remaining problem by a constant fraction, usually in half. Doubling the input adds only one more step. Binary search in a sorted list is the classic example: a million items need about 20 comparisons.
Why do we drop constants in Big O?
Big O describes the growth rate as the input gets large, and at large sizes the fastest-growing term dominates everything else. An algorithm that does n² + 3n + 4 steps is O(n²), because for big n the extra 3n + 4 hardly matters. Constants still matter in practice, but they do not change the class.
What is the difference between time complexity and space complexity?
Time complexity describes how the number of steps grows with the input. Space complexity describes how the extra memory grows. Both are written in Big O notation, and the two often trade off: an index or a cache uses more memory to make lookups faster.
Read next
- Types of Software Testing: Unit, Integration, E2E and More
- How Does the Internet Work? Every Networking Concept Explained
- What Is a WebSocket? WebSocket vs SSE vs Polling vs HTTP
- Authentication vs Authorization: Sessions, JWT, OAuth, SSO
- What Is Docker? Docker and Kubernetes Explained Simply
- Software Engineering Unpacked: home
- The CCAR-F video playlist on YouTube