Summary
Time complexity tells you how the work of an algorithm grows as the input grows. We write it with Big O notation, like O(n) or O(n squared). It ignores exact timings and counts how fast the steps pile up. This helps you pick code that stays fast even when the data gets huge.
Time complexity sounds scary, but the idea is simple. It answers one question. If your data gets ten times bigger, does your code get ten times slower, or a thousand times slower?
Big O notation is just the shorthand for that answer. Once you can read it, you can look at two solutions and know which one will survive big data. Let me make it click with a table you can feel.
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 you have a list of n items. One algorithm might take n steps to finish. Another might take 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. Same task, wildly different fate.
That is why we care about time complexity. It predicts whether your code stays quick or grinds to a halt as the input climbs. It is a core idea in any DSA roadmap.
What is Big O notation?
Big O notation is how we write time complexity. You will see it as O followed by a formula in brackets, like O(n) or O(n squared). The letter n stands for the size of the input. The formula inside says how the steps grow with n.
Big O keeps only the part that matters most for large inputs. So if an algorithm takes 3n plus 5 steps, we just call it O(n), because the 3 and the 5 stop mattering when n is huge. Big O cares about the shape of the growth, not the small details.
Read it out loud as the growth. O(n) means the work grows in line with the input. O(n squared) means it grows like the input times itself. That reading is all you need to start.
The growth table that makes it click
Definitions only go so far. The fastest way to feel time complexity is to see real numbers. This table shows roughly how many steps each class needs as the input n grows from 10 to 1,000.
| 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) | 30 | 700 | 10,000 |
| O(n squared) | 100 | 10,000 | 1,000,000 |
Look at the last column. As n hits 1,000, O(1) still does one step while O(n squared) does a million. That is the whole reason we study this. The class you pick decides whether big data is easy or impossible.
The common time complexity classes
Here are the classes you will meet most, from fastest to slowest. O(1) is constant time. The work never changes with input size. Reaching an item in an array by its index is O(1).
O(log n) is logarithmic time. The work grows very slowly, since each step throws away half the data. Binary search is the classic example. O(n) is linear time, where the work matches the input, like a linear search that checks every item once.
O(n log n) is the speed of the good sorts like merge sort. O(n squared) is the slow sorts like bubble sort, where nested loops walk the data twice over. Learn these five and you can read most code.
What Big O does not tell you
Big O hides the constants. An O(n) algorithm that does heavy work each step can be slower than an O(n log n) one on small inputs. So Big O predicts how code scales as data grows, not how fast it runs on any single input. For small data, the simple choice often wins even with a worse Big O. Use Big O to plan for growth, then test with real data to know the true speed.
What Big O hides
The box above holds the lesson most tutorials skip, so it is worth repeating. Big O drops constants and small terms. It tells you the trend, not the stopwatch time. Two O(n) algorithms can differ a lot in real speed.
This matters in practice. On a tiny list, an O(n squared) sort can beat a fancy O(n log n) one, because the fancy one has more setup per step. That is why real sorts switch to simple methods for small pieces. So read Big O as a guide to growth, then measure real data before you trust raw speed.
FAQ
What is time complexity in simple terms?
It is how the number of steps grows as the input grows. It does not measure seconds. It measures whether bigger input means a little more work or a lot more work.
What is Big O notation?
It is the way we write time complexity, like O(n) or O(n squared). The n is the input size, and the formula says how the steps grow. Big O keeps only the part that matters for large inputs.
What is the best time complexity?
O(1) constant time is best, since the work never grows. After that comes O(log n), then O(n), then O(n log n), then O(n squared). Lower on this list means slower as data grows.
Why does Big O ignore constants?
Because for large inputs the growth shape matters far more than a fixed multiplier. An O(n) algorithm beats an O(n squared) one at scale no matter the constants. Big O focuses on that trend.
What is the difference between time and space complexity?
Time complexity measures how the steps grow. Space complexity measures how the extra memory grows. Both use Big O notation, and you often trade one for the other.
Is O(n log n) good?
Yes, it is very good for sorting. It is much faster than O(n squared) and only a little slower than O(n). The best general sorts run in O(n log n).
So what should you remember?
Time complexity is about growth, not seconds. Big O notation writes that growth as a simple formula, and the five common classes from O(1) to O(n squared) cover most of what you will meet. The growth table shows why the class you pick matters so much at scale.
Keep the hidden side in mind too. Big O drops constants, so it predicts scaling, not raw speed. Read it to plan for growth, then test with real data. Master this and every other algorithm topic gets easier.
Now check yourself. An algorithm takes 2n plus 100 steps for an input of size n. What is its time complexity in Big O notation?