Determine \Theta for the following code fragment in the average case. Assume that all variables are of type "int". Assume that array A contains n values, "random" takes constant time, and "sort" takes n \log n time.

              sum = 0;
              for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++)
                  A[j] = random(n);
                sort(A);
              }
            

\Theta(n^2 \log n)
  • \Theta(\log n)
  • \Theta(n^2)
  • \Theta(1)
  • \Theta(n)
  • \Theta(n \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.