Reading preferences
Concept

Time and Space Complexity

Count how work and extra storage grow when the input gets bigger, instead of guessing from a stopwatch.

complexity, big O, linear, quadratic, logarithmic, time, space

Ten boxes or a thousand boxes

Checking ten boxes is a small job. Checking a thousand boxes is bigger. Before running a program, we can ask: how much bigger does the job get when there are more items?

Time complexity describes growth in the amount of work. Space complexity describes growth in storage. Neither tells us whether the answer is correct; that needs separate reasoning.

Why we need it and where it is used

A stopwatch depends on the computer, other running programs, and the input. Counting operations helps compare strategies before choosing one for a large collection. It explains why a simple method can be fine for five numbers but poor for a million.

We use this reasoning when choosing a search or sort, deciding whether to copy a list, or checking whether a loop does much more work than expected. Actual measurements still matter; growth estimates do not replace profiling.

Four useful growth patterns

Let n mean the number of input items. Assume each elementary comparison or list access costs a fixed amount for now.

PatternPlain-language ideaTypical example
O(1)Work bounded independently of nRead one valid Python-list index
O(n)Work grows roughly with nVisit all items once
O(n²)Work can grow roughly with n times nCheck all ordered pairs
O(log n)Repeatedly shrink the remaining region by a fixed factorBinary search in a sorted random-access list

Big O is an upper-bound growth notation for sufficiently large inputs. It ignores constant factors and smaller terms. In common discussions we usually choose a useful tight growth description, even though a loose upper bound can also be mathematically true. O(1) does not mean "exactly one operation".

Compare small counts before symbols

Number of itemsOne visit per itemAll ordered pairs
44 visits16 pair checks
88 visits64 pair checks
1616 visits256 pair checks

Doubling the items doubles the visits but quadruples the pair checks. For halving, the number of halvings grows much more slowly: a region of 16 items can be reduced through sizes 8, 4, 2, and 1. A final check may still be needed. That is the intuition behind logarithmic growth, not an exact comparison count.

Python illustration

def pair_count(values):
    checks = 0
    for first in values:
        for second in values:
            checks += 1
    return checks

print(pair_count([1, 2, 3, 4]))
print(pair_count([1, 2, 3, 4, 5, 6, 7, 8]))

Prepared output — not live execution:

16
64

For every choice of first, the inner loop visits every second, including the same item. Two separate loops one after the other would instead make n + n visits, which is O(n), not O(n²).

Best case and worst case

A search may find the target immediately or only at the end. Best case describes the least work among allowed inputs of a given size. Worst case describes the most. An average case needs an explicit assumption about which inputs are likely; it is not just halfway between best and worst.

Always say which case and cost model a claim describes. For example, Binary Search is O(log n) in the worst case only when the data is already sorted and the middle item is efficiently accessible.

Extra storage is not the input itself

An input list already exists. A few counters or remembered values use O(1) auxiliary space, meaning extra storage beyond that input. A new list of n results uses O(n) extra space. Total space including the original list can therefore differ from auxiliary space.

These beginner counts assume fixed-size operations. Very long text comparisons, Python's growing integers, allocation, and the chosen data structure can change the real cost. Faster growth does not mean a method is slower for every tiny input.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What does a time-complexity description tell us?
2. If a procedure visits every item once, how does its work usually grow?
3. A procedure checks every ordered pair of n items. About how many pair checks are there?
4. What changes when a traversal stores all n items in a new list?

References and further reading