-
Consider the following function :
int unknown (int n) {
int i, j, k=0;
for (i=n/2; i<=n; i++)
for (j=2; j<=n; j=j*2)
k = k + n/2;
return (k);
}
The return value of the function is
-
- Θ(n2)
- Θ(n2 log n)
- Θ(n3)
- Θ(n3 log n)
- Θ(n2)
Correct Option: B
The return value of the function is q (n2 log n)
The outer for loop goes | + 1 iterations. | |
2 |
The inner for loop runs independent of the outer loop.
And for each inner iteration, | gets added to k. | |
2 |
∴ | × #outer loops × #inner loops per outer loop. | |
2 |
#Inner loops = θ (log n) [∵ 2θ(log n) = θ (n)]
∴ | × | + 1 | . θ (log n) = θ (n2 log n) | ||||
2 | 2 |