Quick Answer
Time complexity has 5 common classes in Big O. They are O(1), O(log n), O(n), O(n log n) and O(n squared). Time complexity counts steps and not seconds. Big O drops constants so 3n plus 5 becomes O(n). O(n) work doubles when input doubles. O(n squared) work quadruples when input doubles. Test real data before you trust raw speed.
Time complexity tells you how the work of an algorithm grows as the input grows. It counts steps and not seconds.
Big O notation is the shorthand for that answer. Look at two solutions and Big O shows which one survives big data.
Real numbers from Python code follow below. Doubling the input exposes every class.
What Is Time Complexity?
Time complexity measures how the number of steps in an algorithm grows as the input grows. It does not measure seconds. It measures the shape of the growth, which matters far more when data gets big.
Say a list has n items. One algorithm takes n steps and another takes n times n steps.
For ten items the gap is small. For a million items one takes a million steps and the other takes a trillion.
Growth class predicts if code remains quick or grinds to a halt as input climbs. Every DSA roadmap teaches it early for that reason.
What Is Big O Notation?
Big O notation is how you state time complexity. Look for O followed by a formula in brackets, like O(n) or O(n squared). Letter n stands for the size of the input.
Big O counts only the part that matters most for large inputs. With 3n plus 5 steps an algorithm is O(n) because the 3 and the 5 stop mattering when n is huge.
Say it out loud as growth. O(n) means the work grows in line with the input. O(n squared) means the work grows like the input times the input.
Find the full reference in the Big O lesson.
How Does Time Complexity Grow as Input Grows?
Use the table to see why the class decides if big data is possible. Step counts are rounded and log means base 2.
| Class | n = 10 | n = 100 | n = 1,000 |
|---|---|---|---|
| O(1) | 1 | 1 | 1 |
| O(log n) | 3 | 7 | 10 |
| O(n) | 10 | 100 | 1,000 |
| O(n log n) | 33 | 664 | 9,966 |
| O(n squared) | 100 | 10,000 | 1,000,000 |
Look at the last column. At n = 1,000 the O(1) class still takes one step while O(n squared) takes a million. That gap is the reason to study growth classes.
Which 5 Time Complexity Classes Matter Most?
Five classes explain most code you meet. They are listed from the lowest growth to the highest growth.
- O(1) constant time. Work is the same at any input size. Reaching an item in an array by its index is O(1).
- O(log n) logarithmic time. Every step throws away half the data. Binary search is the classic example.
- O(n) linear time. Work matches the input, like a linear search that checks every item once.
- O(n log n) is the speed of good sorts like merge sort.
- O(n squared) is the speed of slow sorts like bubble sort, where nested loops walk the data twice over.
Python code below shows one function for every class.
def first_item(items):
return items[0] # O(1)
def binary_search(items, target): # O(log n), items must be sorted
low, high = 0, len(items) - 1
while low <= high:
mid = (low + high) // 2
if items[mid] == target:
return mid
if items[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
def total(items): # O(n)
result = 0
for item in items:
result += item
return result
def sort_items(items): # O(n log n)
return sorted(items)
def count_smaller_pairs(items): # O(n squared)
count = 0
for a in items:
for b in items:
if a < b:
count += 1
return count
Do You Know? Binary search needs a sorted list. On an unsorted list it can return a wrong answer.
Does Doubling the Input Show the Growth Class?
Yes. Double the input and watch the time. O(n) work doubles and O(n squared) work quadruples.
| Code | Input size | Time | Ratio to the row above |
|---|---|---|---|
| total, O(n) | 1,000,000 | 21.0 ms | - |
| total, O(n) | 2,000,000 | 39.9 ms | 1.9 times |
| total, O(n) | 4,000,000 | 76.7 ms | 1.9 times |
| count_smaller_pairs, O(n squared) | 1,000 | 30 ms | - |
| count_smaller_pairs, O(n squared) | 2,000 | 124 ms | 4.1 times |
| count_smaller_pairs, O(n squared) | 4,000 | 496 ms | 4.0 times |
Tested. Python 3.13 executed both functions in a cloud sandbox and the table shows the median of several timings. Times differ on your computer but the ratios remain near 2 and 4.
Heads Up. Extremely short timings give noisy ratios. Choose input sizes that take at least a few milliseconds.
What Does Big O Hide?
Big O drops constants and small terms. It tells you the trend and not the stopwatch time. Two O(n) algorithms can differ a lot in real speed.
Table below compares insertion sort, which is O(n squared), with merge sort, which is O(n log n). Both are pure Python versions timed in the same sandbox.
| Input size | Insertion sort | Merge sort |
|---|---|---|
| 8 | 0.0013 ms | 0.0036 ms |
| 16 | 0.0037 ms | 0.0093 ms |
| 2,000 | 63.1 ms | 2.5 ms |
On tiny lists the O(n squared) sort wins because merge sort does more setup per step. At 2,000 items merge sort wins by about 25 times.
Real sorting code follows the same idea. According to the CPython sort notes, Python sorts arrays shorter than 64 items with binary insertion sort. Use Big O to plan for growth and then test with real data.
Frequently Asked Questions
Does Time Complexity Measure Seconds?
No. It counts how the number of steps grows as input grows. Seconds depend on the computer and the language.
Is Big O the Same as Time Complexity?
No. Time complexity is the idea and Big O is the way to express it, like O(n) or O(n squared).
Is O(1) the Best Time Complexity?
Yes. Work never grows with input. After O(1) come O(log n), O(n), O(n log n) and O(n squared) in order of growing work.
Does Big O Notation Ignore Constants?
Yes. For large inputs the growth shape matters far more than a fixed multiplier. O(n) code beats O(n squared) code at scale no matter the constants.
Are Time Complexity and Space Complexity the Same?
No. Time complexity measures how the steps grow. Space complexity measures how the extra memory grows. Both use Big O and you trade one for the other.
Is O(n log n) Good?
Yes. It is far better than O(n squared) and only a little above O(n). Merge sort is a common example.
Can You Find the Growth Class by Doubling the Input?
Yes. Time the code at size n and at size 2n. Ratios near 2 point to O(n) and ratios near 4 point to O(n squared).
What Should You Remember About Time Complexity?
Growth and not seconds defines the topic. Big O expresses that growth as a formula and 5 classes from O(1) to O(n squared) describe most code you meet.
Key takeaways.
- O(n) work doubles when the input doubles.
- O(n squared) work quadruples when the input doubles.
- Big O predicts scaling and does not predict raw speed on small input.
Practice question. Take an algorithm with 2n plus 100 steps for input size n. Find its time complexity. Answer. O(n) because Big O drops the 2 and the 100.
Watch growth live in the Binary Search visualiser and the Merge Sort visualiser. Solve more in the DSA practice set.