āŒ‚ ≔

Time Complexity

Time complexity means how the amount of work grows when the input gets bigger. It is not the exact number of seconds. It is the growth pattern of the algorithm.

Time = amount of work
More input means more steps
Input size is often n

Simple Idea

If an algorithm checks each item once, then more items mean proportionally more work. That is why many simple scans are O(n). If the algorithm uses nested loops over the same data, work can grow much faster.

Why It Matters

For a small list, almost any method feels fast. For a very large list, the growth rate matters a lot. A method that does too many repeated comparisons becomes slow much sooner.

Simple Examples

Example 1: O(1)

Open a student register and read the name written in the first row. It is one direct action, whether the register has 20 students or 2,000 students.

Example 2: O(n)

A teacher checking attendance by calling every student's name one by one is doing O(n) work. More students means more names to call.

Example 3: O(log n)

Looking for a word in a dictionary is like O(log n). You open near the middle, decide whether to go left or right, then cut the remaining search area in half again.

Example 4: O(n²)

Imagine comparing every student in a class with every other student to see who should stand earlier in a line. That repeated pair-by-pair checking grows very quickly, like O(n²).

O(log n) With A Real Example

Think about finding a word in a printed dictionary. You do not start from page 1 and turn every page. You open near the middle, check the word, then immediately ignore half of the dictionary. After that, you again ignore half of what is left. That is why Binary Search is O(log n).

Real Situation
About How Many Checks
What Happens
A very small dictionary
About 3 checks
You open near the middle, then choose the left or right half a few times.
A dictionary about twice as thick
About 4 checks
Even though the book is much bigger, you only need about one extra decision.
A much larger dictionary
About 5 checks
The book grows a lot, but your search still stays short because you keep halving it.
A huge dictionary in a library
About 10 checks
You still do only a small set of checks because each step removes half of the remaining pages.

Easy Memory Trick

Ask this question:

"If the input gets bigger,
how many more steps will this algorithm do?"

One direct action: often O(1)
Cut the remaining work in half each time: often O(log n)
Check everything once: often O(n)
Compare many pairs again and again: often O(n^2)