Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. 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
    1. Θ(n2)
    2. Θ(n2 log n)
    3. Θ(n3)
    4. Θ(n3 log n)
Correct Option: B

The return value of the function is q (n2 log n)

The outer for loop goes
n
+ 1 iterations.
2

The inner for loop runs independent of the outer loop.
And for each inner iteration,
n
gets added to k.
2

n
× #outer loops × #inner loops per outer loop.
2

#Inner loops = θ (log n) [∵ 2θ(log n) = θ (n)]
n
×
n
+ 1 . θ (log n) = θ (n2 log n)
22



Your comments will be displayed only after manual approval.