Bitpath

Articles / Learn

Big-O in five minutes

Big-O answers one question about your code. The whole idea, with the five growth rates you will actually meet, taken from the first unit of Algorithm Foundations.

Big-O answers one question: when the input gets bigger, how much more work does the code do?

It counts steps, not seconds. That is why the answer is the same on a phone and on a supercomputer, and why it is the first thing in Bitpath’s first roadmap.

Count the loop

Take a function that looks for a name in a list.

def contains(names, target):
    for name in names:
        if name == target:
            return True
    return False

If the list holds 1,000 names and the target is not there, the loop checks all 1,000. With a million names, a million checks. The work grows in step with the input. That is linear time, written O(n), where n is the size of the input.

Now a function that returns the first item:

def first(items):
    return items[0]

Five items or five million, it does one thing. No dependence on n at all. That is constant time, O(1).

Loops inside loops

def has_duplicate(nums):
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            if nums[i] == nums[j]:
                return True
    return False

For each item, the inner loop looks at the rest. For n items that is roughly n × n / 2 comparisons. Double the input and the work quadruples. That is quadratic time, O(n²).

Two rules cover most code you will read:

  • Loops one after another add. n + n steps is still O(n).
  • Loops inside each other multiply. n × n steps is O(n²).

Then simplify

Big-O keeps only what matters when n is large.

  • Drop constants: 3n + 10 is O(n).
  • Drop smaller terms: n² + n is O(n²).

At a million items, n² is a trillion and the extra n is a rounding error. Only the fastest-growing term survives.

Halving is the cheap trick

Guess a number between 1 and 1,000,000. If every guess is answered with “higher” or “lower”, you need at most 20 guesses, because each one throws away half of what is left.

Any algorithm that halves the problem at every step is logarithmic, O(log n). It is the idea behind binary search, and it is why a sorted list is so valuable: 20 steps instead of a million.

The ladder

From best to worst, the growth rates you will meet most:

Big-O Name 1,000 items take about
O(1) constant 1 step
O(log n) logarithmic 10 steps
O(n) linear 1,000 steps
O(n log n) the good sorts 10,000 steps
O(n²) quadratic 1,000,000 steps

Unless someone says otherwise, Big-O means the worst case: the input that makes the code work hardest. For contains, that is the missing name.

Memory is counted the same way. A few variables are O(1) space. A new list as long as the input is O(n) space.

Try it

Reading about counting steps is not the same as counting them. The first lesson of Algorithm Foundations asks you to do it, one small question at a time, and the whole unit ends with a boss: guessing a number in twenty questions.