Search code examples
algorithmtime-complexitybig-ocomplexity-theory

What is the complexity(Ø) of this pseudo code?


My question is about finding the complexity of this algorithm. J value is related to n, so I'm confused about this.

What is the asymptotic complexity of this pseudocode?

for i=1 to n
  do
  j = 1;
  while (j < n)
    do
    j = j * 2;

Thanks.


Solution

  • I believe it is O(n log2n)

    The outer loop is called n times and the inner loop is called log2n times, since in every iteration, j is doubled. For first iteration, i.e., k=0; j is equal to 1 and goes on like 2, 4, 8, ... until 2k>=n