-6

Compute the complexity of the following Algorithm.

i = 1;
while(i < n+1)
{
   j=1
   while(j < n+1) 
   {
      j = j*2
   }
   i++
}
Shane_Yo
  • 770
  • 8
  • 24

1 Answers1

0

Ask yourself, in which way does i increment grow towards the final value n? How many times will the outer loop run for a given n?

The same for the inner loop. I recommend you read through somthing like this or this SO post and maybe start with some examples:

n = 100;
i = 1;
while (i < n+1){
    j = 1;
    while (j < n+1) {
        j = j*2
    }
    i = i+1;
}

How many times exactly will both of the loops run?

Community
  • 1
  • 1
adrianus
  • 3,141
  • 1
  • 22
  • 41
  • That were some hints already. Read through the link provided or do your own google search with `time complexity of nested loops` and tutorials will pop up everywhere (even for your exact problem). – adrianus Jul 15 '15 at 08:02