Big-O notation describes how the running time (or memory use) of an algorithm grows as the input gets bigger. It ignores constants and hardware and keeps only the part that dominates for large inputs. For example, checking every item in a list is O(n): twice the items, twice the work. Binary search on a sorted list is O(log n): a million items need only about 20 steps.
This guide explains the common complexity classes with code examples, shows how to calculate Big-O yourself, and lists the complexity of the data structures you use every day in Python, Java, C++ and SQL. Start with the interactive lab to see the difference with your own eyes.
Interactive Big-O lab
In Lab 1, change the number of items and watch how many steps each complexity needs. In Lab 2, step through a binary search and compare it with a linear search.
The common Big-O complexity classes
| Big-O | Name | Steps for n = 1,000,000 | Typical example |
|---|---|---|---|
O(1) | Constant | 1 | Array index, hash table lookup |
O(log n) | Logarithmic | about 20 | Binary search, balanced tree lookup |
O(n) | Linear | 1,000,000 | Loop over a list, linear search |
O(n log n) | Linearithmic | about 20,000,000 | Merge sort, Timsort, efficient sorting |
O(nยฒ) | Quadratic | 1,000,000,000,000 | Nested loops, bubble sort |
O(2โฟ) | Exponential | impossible | Trying every subset |
The jump between classes matters far more than any constant factor. An O(nยฒ) algorithm on fast hardware still loses to an O(n log n) algorithm on slow hardware once the input is large enough.
Big-O with code examples
# O(1): one step, no matter how big the list is
first = items[0]
# O(n): one pass over the list
total = 0
for x in items:
total += x
# O(nยฒ): a loop inside a loop over the same list
for a in items:
for b in items:
if a + b == target:
print(a, b)
# O(log n): the problem halves every step
lo, hi = 0, len(items) - 1
while lo <= hi:
mid = (lo + hi) // 2
if items[mid] == target:
break
elif items[mid] < target:
lo = mid + 1
else:
hi = mid - 1
How to calculate Big-O
- Count the loops. One loop over n items is O(n). A loop inside a loop over the same data is O(nยฒ).
- Steps in sequence add up, nested steps multiply. Two separate loops are O(n + n) = O(n). Nested loops over two different inputs are O(n ร m), not O(nยฒ).
- Drop constants. O(2n) and O(n + 100) are both O(n).
- Keep only the dominant term. O(nยฒ + n) is O(nยฒ), because nยฒ dominates for large n.
- Halving means log n. If every step cuts the remaining work in half, the loop runs about logโ n times.
- Watch for hidden loops.
x in my_list,list.contains()andstd::findare loops too.
Data structure complexity cheat sheet
| Structure | Access | Search | Insert | Delete |
|---|---|---|---|---|
| Array / dynamic array | O(1) | O(n) | O(1) amortized at the end, O(n) in the middle | O(n) |
| Linked list | O(n) | O(n) | O(1) at the head | O(1) with a reference to the node |
| Hash table | - | O(1) average, O(n) worst | O(1) average | O(1) average |
| Balanced binary search tree | O(log n) | O(log n) | O(log n) | O(log n) |
| Binary heap | O(1) for min or max | O(n) | O(log n) | O(log n) for min or max |
| Stack / queue | O(1) for the top or front | O(n) | O(1) | O(1) |
What it looks like in your language
| Language | O(1) average lookup | O(log n), sorted | O(n) lookup (careful) |
|---|---|---|---|
| Python | dict, set | bisect on a sorted list | x in list, list.index() |
| Java | HashMap, HashSet | TreeMap, TreeSet | ArrayList.contains() |
| C++ | std::unordered_map | std::map, std::set | std::find on a vector |
| SQL | Hash index (some databases) | B-tree index lookup | Full table scan without an index |
This is why a single index can turn a slow query into a fast one: without an index the database scans every row (O(n)), with a B-tree index it walks down a tree (O(log n)).
Sorting algorithm complexity
| Algorithm | Best | Average | Worst | Extra space | Stable |
|---|---|---|---|---|---|
| Bubble sort | O(n) | O(nยฒ) | O(nยฒ) | O(1) | Yes |
| Insertion sort | O(n) | O(nยฒ) | O(nยฒ) | O(1) | Yes |
| Merge sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quicksort | O(n log n) | O(n log n) | O(nยฒ) | O(log n) | No |
| Heap sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
| Timsort (Python, Java objects) | O(n) | O(n log n) | O(n log n) | O(n) | Yes |
Big-O is not the whole story
Two O(n) programs can still differ ten times in speed. Big-O hides constants, and real hardware adds its own: walking through an array is fast because the items sit next to each other in memory and the CPU cache loads them in blocks, while a linked list jumps around memory. Knowing how the CPU, memory and caches work is what turns "the right complexity" into genuinely fast code.
Frequently asked questions
What is Big-O notation in simple terms?
Big-O notation describes how the work an algorithm does grows as its input grows. O(n) means the work grows in proportion to the input, O(nยฒ) means doubling the input quadruples the work, and O(1) means the work stays the same regardless of input size.
What is the difference between O(n) and O(log n)?
O(n) work grows in step with the input: a million items need about a million steps. O(log n) work grows very slowly because each step halves the problem: a million items need only about 20 steps, which is why binary search is so fast on sorted data.
How do you calculate the Big-O of a function?
Count how many times the main operations run as a function of the input size. Add the cost of steps in sequence, multiply the cost of nested loops, drop constant factors and keep only the fastest-growing term. Remember that built-in operations such as searching a list are loops too.
Is O(1) always faster than O(n)?
Not for small inputs. O(1) means the cost does not grow, but that constant cost can be large, such as computing a hash. For a handful of items a simple O(n) loop can be faster. For large inputs, the lower complexity always wins.
What is the time complexity of a hash table lookup?
On average a hash table lookup, insert or delete is O(1). In the worst case, when many keys collide, it can degrade to O(n). Modern implementations such as Java HashMap reduce the worst case by turning crowded buckets into balanced trees.
Why does Big-O matter in coding interviews?
Interviewers use Big-O to see whether you can reason about how a solution scales. Being able to state the time and space complexity of your code, and to improve it, for example from O(nยฒ) to O(n) with a hash set, is often as important as getting the right answer.
Where to go next
Big-O tells you how work grows. To understand why some code is still faster than other code with the same complexity, you need to know how the computer executes it: how the CPU, memory and input and output communicate, how numbers are stored and added in binary, and how logic gates build a processor. That is covered lesson by lesson in Low-Layer Computer Understanding and Hardware Foundations.


