6) Determine for the following code fragments in the average case. Assume that all variables are of type int. (a) (b) (c) a = b + c; d = a +e; sum = 0; for (i=0; i<3; i++) for (j=0; j

icon
Related questions
Question

Help me please 

6) Determine for the following code fragments in the average case. Assume that all variables
are of type int.
(a)
(b)
O
(d)
(f)
a = b + c;
d = a +e;
sum = 0;
for (i=0; i<3; i++)
(i)
for (j=0; j<n; j++)
sum++;
sum=0;
for (i=0; i<n*n; i++)
sum++;
for (i=0; i<n-1; i++)
for (j=i+1; j<n; j++) {
tmp = AA[i][j];
AA[i][j] = AA[j][i];
AA[j][i] = tmp;
}
sum = 0;
for (i=1; i<n; i++)
for (j=1; j<n; j*=2)
sum++;
sum = 0;
for (i=1; i<n; i*=2)
for (j=1; j<n; j++)
sum++;
(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++)
}
(h) Assume array A contains a random permutation of the values from 0 to n - 1.
sum = 0;
for (i=0; i<n; i++)
A[i] = util.random(n);
sort(A);
for (j=0; A[j]!=i; j++)
sum++;
sum = 0;
if (EVEN(n))
for (i=0; i<n; i++)
sum++;
else
sum sum +n;
Transcribed Image Text:6) Determine for the following code fragments in the average case. Assume that all variables are of type int. (a) (b) O (d) (f) a = b + c; d = a +e; sum = 0; for (i=0; i<3; i++) (i) for (j=0; j<n; j++) sum++; sum=0; for (i=0; i<n*n; i++) sum++; for (i=0; i<n-1; i++) for (j=i+1; j<n; j++) { tmp = AA[i][j]; AA[i][j] = AA[j][i]; AA[j][i] = tmp; } sum = 0; for (i=1; i<n; i++) for (j=1; j<n; j*=2) sum++; sum = 0; for (i=1; i<n; i*=2) for (j=1; j<n; j++) sum++; (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++) } (h) Assume array A contains a random permutation of the values from 0 to n - 1. sum = 0; for (i=0; i<n; i++) A[i] = util.random(n); sort(A); for (j=0; A[j]!=i; j++) sum++; sum = 0; if (EVEN(n)) for (i=0; i<n; i++) sum++; else sum sum +n;
Expert Solution
steps

Step by step

Solved in 4 steps

Blurred answer