Logarithmic Runtime

In this lesson, we'll discuss when an algorithm can have a logarithmic runtime.

Iterating powers of a number

Let’s analyze the loop below where we iterate over all powers of 22

for (int i = 1; i <= N; i *= 2)
    x++;
  • Iteration 11: i=1i=1
  • Iteration 22: i=2i=2
  • Iteration 33: i=4i=4
  • Iteration xx: i=2x1i=2^{x-1}

Let’s say the loop terminates after the jthj^{th} iteration, i.e.,

2j1<=N2^{j-1} <= N

j1<=logNj-1 <= logN

j<=logN+1j <= logN + 1

Hence, the loop runs (logN+1)(logN + 1) times.

In Big-O notation, the time complexity is O(logN)O(logN)


A similar analysis gives O(logN)O(logN) runtime for the loop below.

for (int i = N; i >= 1; i /= 2)
      x++;

Harmonic series

Consider the piece of code below:

for (int i = 1; i <= N; i++)
    for (int j = i; j <= N; j += i)
        x++;

For all integers between 11 and NN, we iterate over their multiples. The number of operations, therefore, will be:

N+N2+N3+N4+...+NN1+1N + \frac{N}{2} + \frac{N}{3} + \frac{N}{4} + ... + \frac{N}{N-1} + 1

=N[1+12+13+...+1N1+1N]= N[1 + \frac{1}{2} + \frac{1}{3} + ... + \frac{1}{N-1} + \frac{1}{N}]

Let’s define an upper bound on the second term. Grouping the terms, we get:

First term: 1<=11 <= 1

Second term: 12+13<=12+12<=1\frac{1}{2} + \frac{1}{3} <= \frac{1}{2} + \frac{1}{2} <= 1

Third term: 14+15+16+17<=14+14+14+14<=1\frac{1}{4} + \frac{1}{5} + \frac{1}{6} + \frac{1}{7} <= \frac{1}{4} + \frac{1}{4} + \frac{1}{4} + \frac{1}{4} <= 1

and so on.

Let kk be the number of groups we can make, then

1+2+4+...+2k1<=N1 + 2 + 4 + ... + 2^{k-1} <= N

2k1<=N2^k - 1 <= N

2k<=N+12^k <= N + 1

k<=log(N+1)k <= log(N+1)

Coming back to the number of operations, we have

N[1+12+13+...+1N1+1N]<=Nlog(N+1)N[1 + \frac{1}{2} + \frac{1}{3} + ... + \frac{1}{N-1} + \frac{1}{N}] <= N*log(N+1)

Therefore, the time complexity is O(NlogN).O(NlogN).


In the next lesson, we’ll discuss an example where the runtime is not what it seems at a first glance.

Get hands-on with 1400+ tech skills courses.