Sorting four numbers tells us whether an algorithm works on four numbers. It tells us little about how long the same method will take on four million. To think about that, we need a way to describe how its work grows with the size of its input.
Background for this page. Here n means the number of input items. A growth bound describes a resource as n increases; it is not an exact number of seconds. Related explanations: Summations for algorithm analysis · Algorithms: inputs, outputs, and correctness.
3.1 Count an operation you can see
Suppose a list has n items. A search that checks each item once may make at most n comparisons. If we double the list, the maximum number of checks doubles. A method that compares every pair behaves differently. With four items, there are 6 distinct pairs. With eight, there are 28. With sixteen, there are 120.
This function counts pairs of equal values. It checks each distinct pair once, even when there are no equal values:
# Count pairs whose values are equal.
def count_equal_pairs(values):
# Begin with no matches.
matches = 0
# Choose the first position in a pair.
for left in range(len(values)):
# Choose only later positions to avoid repeats.
for right in range(left + 1, len(values)):
# Count this pair if its values match.
if values[left] == values[right]:
matches += 1
# Return the number of matching pairs.
return matches
For n items, the first item has n − 1 possible partners, the next has n − 2, and so on. Add those counts and you get n(n − 1)/2 comparisons. The result might be zero, but the function still makes all those comparisons. We count the work the method does, not only the matches it finds.
| Number of items, n | One check per item | About n log₂ n checks | Compare every pair |
|---|---|---|---|
| 4 | 4 | 8 | 6 |
| 8 | 8 | 24 | 28 |
| 16 | 16 | 64 | 120 |
The middle column gives a useful picture of the growth of algorithms such as merge sort; it is not an exact count for every implementation. The table shows why the shape of growth matters. A method can be quick on a tiny input and still become a poor choice when the input grows.
3.2 What the letters mean
We often ignore fixed multipliers and smaller terms so we can see the main shape. The pair count n(n − 1)/2 is roughly half of n² for large n. We describe its growth as Θ(n²), read “theta of n squared.” Θ means a tight bound: above and below, the work grows at that rate up to constant factors once n is large enough.
O(n²) is an upper bound. It says the work does not eventually grow faster than a constant times n². Ω(n²) is a lower bound. It says the work does not eventually grow slower than a constant times n². When both describe the same growth, Θ gives the tight description. In the formal definitions, “eventually” means that there is some input size n₀ after which the bound holds, and a fixed positive constant can multiply the comparison function. The constants let us compare growth without pretending that every machine takes the same time for one step.
An O bound is not automatically a worst-case statement. We must say which quantity we are bounding. Insertion sort’s worst-case time is O(n²), while its best-case time is O(n). It is also true, though less useful, that an O(n) method is O(n²). An upper bound need not be the tightest one. When you read a complexity claim, look for both the case being discussed and whether the bound is tight.
3.3 Useful shapes and their limits
You will meet constant growth, written O(1), when an operation takes a bounded amount of work regardless of the number of stored items. Logarithmic growth, O(log n), often appears when a method repeatedly cuts the remaining possibilities by a fixed fraction. Linear growth, O(n), appears when every item must be visited. Sorting by comparison commonly gives us n log n methods. Nested work can lead to quadratic growth, O(n²). Some problems produce exponential growth, which becomes difficult much sooner.
These labels describe a trend, not a stopwatch reading. They leave out constant costs, memory use, input shape, and how the program is actually written. For small inputs, a simpler method with a worse growth rate may still be the sensible choice. For a large input, asymptotic growth becomes much more important. We use the notation to ask better questions, then measure real implementations when performance matters.
The same notation can describe memory. The pair-counting function keeps only a few extra values as it runs, so its extra space is O(1), even though it does Θ(n²) comparisons. Time and space answer different questions.
State which case you are measuring
A search that stops at its first match makes one comparison when the first item matches, but n when the value is absent. Its best case is Θ(1), and its worst case is Θ(n). An average needs a probability model. If a unique match is equally likely at each position, the average comparisons are (n+1)/2, still Θ(n). Name the counted operation too: comparing fixed-size numbers can take constant time, while comparing long strings may examine many characters.
Practice. How many distinct pairs are there in a list of five items? What about ten? Does the pair-counting function make fewer comparisons when every value is different?
Answer. The formula n(n − 1)/2 gives 10 pairs for five items and 45 for ten. The function checks every pair in either case. Different values change the returned count, not the number of comparisons.
Next we will look at a method whose structure gives a different kind of growth: divide a problem into smaller parts, solve them, and put the answers together.
Try another case. Is 7n+20 both O(n) and O(n²)?
Reasoning. Yes. Both are upper bounds, but O(n²) is loose. Θ(n) states the tight growth rate.