Why Doesn’t a Manual Sum Match a Θ(n log n) Loop?
At a glance
- 1. Separate exact counts
Count the loop’s actual iterations or operations before using asymptotic notation.
- 2. Write a summation
Add the work for each iteration, including ceilings, floors, and loop overhead if needed.
- 3. Simplify the growth
Use bounds to determine the asymptotic class; Θ(n log n) does not specify an exact count.
- 4. Check a small input
Evaluate both the code and the proposed formula at a small n to expose indexing or rounding differences.
A program’s operation count and a formula such as are not automatically the same thing. The key distinction is that describes how a quantity grows as becomes large; it does not give the exact number of operations for every input size.
To compare a manual sum with a loop, first count the work performed for each value of the loop variable. Sum those counts, then use asymptotic notation to describe the resulting growth. The exact sum may differ from even when both have the same asymptotic order.
What Θ(n log n) tells you
A function is in if, for sufficiently large , it is bounded above and below by positive constant multiples of . In other words, there are constants and a threshold such that
for all .
This notation intentionally ignores constant factors and lower-order terms. For example, and are both in . They are not the same function. The base of the logarithm also changes the value by a constant factor: .
So if a program is described as taking time, that statement alone cannot be used as an exact term in a summation. You need a more precise count of its work.
Count the work performed by the loop
Suppose a loop has iterations indexed by . If iteration takes operations, the total is
The sequence depends on the actual loop bounds and body. If every iteration does work, then summing over iterations gives . But if the work changes with , you must sum those changing costs; multiplying one representative cost by may be wrong.
For instance, if iteration takes operations, the total is , which grows as , not .
Worked example: a nested loop with a changing step
Consider this pseudocode:
for i = 1 to n:
for j = 1 to n, increasing j by i:
do_constant_time_work()
For a fixed , the inner loop visits up to . Its iteration count is . Therefore the total number of body executions is exactly
This is not exactly . Since ,
The harmonic sum satisfies , so the loop has total cost . The exact count includes the ceilings and is not generally given by a simple estimate.
For , the inner-loop counts are , giving a total of . But , while , which happens to match the exact total for this particular input size. Neither logarithmic expression is generally the exact total. The asymptotic statement is about growth, not equality at each input size.
Common mistakes
- Treating as an equation. It means “same order of growth up to constant factors,” not “equal to .”
- Summing a total-cost bound as though it were per-iteration work. If already describes the whole program, multiplying it by counts the program times.
- Ignoring iteration-dependent bounds. A loop that runs times for iteration is analyzed by summing over , not by assuming every inner loop runs times.
- Dropping rounding in an exact-count question. Ceilings and floors are often irrelevant to the final Θ result, but they matter if you want the literal number of executions.
- Mixing logarithm bases. Changing the base does not change the Θ class, but it does change exact numerical values.
A reliable way to compare a formula with code
Write down the loop’s values and count its body executions for a small input. Then express the general count as a sum, simplify that sum exactly if required, and only afterward reduce it to asymptotic notation. If the question asks for runtime complexity, a Θ bound is usually the goal; if it asks for the exact number of operations, you need the precise loop bounds and a definition of what counts as an operation.
References
- Introduction to Algorithms, 4th edition — Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.