Question 1: Analyze the following codes for Time and space complexity. Determine Big O for the following code fragments in the average case. Assume that all variables are of type int. (g) Assume that array A contains n values, Random takes constant time, and sort takes n log n steps. for (i=0; i
Question 1: Analyze the following codes for Time and space complexity. Determine Big O for the following code fragments in the average case. Assume that all variables are of type int.
(g) Assume that array A contains n values, Random takes constant time, and sort takes n log n steps.
for (i=0; i<n; i++) {
for (j=0; j<n; j++)
A[i] = Random(n);
sort(A, n);
}
(h) Assume array A contains a random permutation of the values from 0 to
n - 1.
sum = 0;
for (i=0; i<n; i++)
for (j=0; A[j]!=i; j++)
sum++;
(i) sum = 0;
if (EVEN(n))
for (i=0; i<n; i++)
sum++;
else
sum = sum + n;
sum1 = 0;
(j) for (k=1; k<=n; k*=2)
for (j=1; j<=n; j++)
sum1++;
sum2 = 0;
for (k=1; k<=n; k*=2)
for (j=1; j<=k; j++)
sum2++;
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images