Determine \Theta for the following code fragment in the average case. Assume that all variables are of type "int".

              sum = 0;
              for (i = 1; i ≤ n; i++)
                for (j = 1; j ≤ n; j *= 2)
                  sum++;
            

\Theta(n \log n)
  • \Theta(\log n)
  • \Theta(n^2)
  • \Theta(1)
  • \Theta(n)
  • \Theta(n^2 \log n)
  • \Theta(n^3)
  • \Theta(2^n)

How much work is done by the body of the inner for loop?

How many times is the inner for loop executed? Multiply this by the amount of work done by the body.

How many times is the outer for loop executed? Multiply this by the amount of work done by the inner for loop.