Cs · Computer Science

Why Doesn’t a Manual Sum Match a Θ(n log n) Loop?

Published By the 9to5Science editorial team

At a glance

  1. 1. Separate exact counts

    Count the loop’s actual iterations or operations before using asymptotic notation.

  2. 2. Write a summation

    Add the work for each iteration, including ceilings, floors, and loop overhead if needed.

  3. 3. Simplify the growth

    Use bounds to determine the asymptotic class; Θ(n log n) does not specify an exact count.

  4. 4. Check a small input

    Evaluate both the code and the proposed formula at a small n to expose indexing or rounding differences.

Advertisement

A program’s operation count and a formula such as nlog⁡nn\log n are not automatically the same thing. The key distinction is that Θ(nlog⁡n)\Theta(n\log n) describes how a quantity grows as nn 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 nlog⁡nn\log n even when both have the same asymptotic order.

What Θ(n log n) tells you

A function T(n)T(n) is in Θ(nlog⁡n)\Theta(n\log n) if, for sufficiently large nn, it is bounded above and below by positive constant multiples of nlog⁡nn\log n. In other words, there are constants c1,c2>0c_1,c_2>0 and a threshold n0n_0 such that

c1nlog⁡n≤T(n)≤c2nlog⁡nc_1n\log n\le T(n)\le c_2n\log n

for all n≥n0n\ge n_0.

This notation intentionally ignores constant factors and lower-order terms. For example, 3nlog⁡2n+5n+73n\log_2 n+5n+7 and nln⁡nn\ln n are both in Θ(nlog⁡n)\Theta(n\log n). They are not the same function. The base of the logarithm also changes the value by a constant factor: log⁡2n=ln⁡n/ln⁡2\log_2 n=\ln n/\ln 2.

So if a program is described as taking Θ(nlog⁡n)\Theta(n\log n) 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 i=1,2,…,ni=1,2,\ldots,n. If iteration ii takes tit_i operations, the total is

T(n)=∑i=1nti.T(n)=\sum_{i=1}^{n} t_i.

The sequence tit_i depends on the actual loop bounds and body. If every iteration does Θ(log⁡n)\Theta(\log n) work, then summing over nn iterations gives Θ(nlog⁡n)\Theta(n\log n). But if the work changes with ii, you must sum those changing costs; multiplying one representative cost by nn may be wrong.

For instance, if iteration ii takes Θ(ilog⁡i)\Theta(i\log i) operations, the total is ∑i=1nΘ(ilog⁡i)\sum_{i=1}^n\Theta(i\log i), which grows as Θ(n2log⁡n)\Theta(n^2\log n), not Θ(nlog⁡n)\Theta(n\log n).

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 ii, the inner loop visits j=1,1+i,1+2i,…j=1,1+i,1+2i,\ldots up to nn. Its iteration count is ⌈n/i⌉\lceil n/i\rceil. Therefore the total number of body executions is exactly

T(n)=∑i=1n⌈ni⌉.T(n)=\sum_{i=1}^{n}\left\lceil\frac{n}{i}\right\rceil.

This is not exactly nlog⁡nn\log n. Since x≤⌈x⌉<x+1x\le\lceil x\rceil < x+1,

n∑i=1n1i≤T(n)<n∑i=1n1i+n.n\sum_{i=1}^{n}\frac{1}{i}\le T(n)<n\sum_{i=1}^{n}\frac{1}{i}+n.

The harmonic sum Hn=∑i=1n1/iH_n=\sum_{i=1}^n 1/i satisfies Hn=ln⁡n+O(1)H_n=\ln n+O(1), so the loop has total cost Θ(nlog⁡n)\Theta(n\log n). The exact count includes the ceilings and is not generally given by a simple nln⁡nn\ln n estimate.

For n=8n=8, the inner-loop counts are 8,4,3,2,2,2,2,18,4,3,2,2,2,2,1, giving a total of 2424. But 8ln⁡8≈16.68\ln 8\approx16.6, while 8log⁡28=248\log_2 8=24, 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 Θ(f(n))\Theta(f(n)) as an equation. It means “same order of growth up to constant factors,” not “equal to f(n)f(n).”
  • Summing a total-cost bound as though it were per-iteration work. If T(n)T(n) already describes the whole program, multiplying it by nn counts the program nn times.
  • Ignoring iteration-dependent bounds. A loop that runs n/in/i times for iteration ii is analyzed by summing over ii, not by assuming every inner loop runs nn 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

  1. 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.