edited by
954 views
0 votes
0 votes

Find the time complexity of the function

function( int n)
{
    int i=1;
    while( i<n)
    {
        int j=n;
        while( j>0)
            j=j/2;
            i=2*i;
    }
}

  1. $O(\log n)$
  2. $O(n^2 \log n )$
  3. $O(\log 2 n)$
  4. $O( \log n^2 )$
edited by

3 Answers

Best answer
1 votes
1 votes

here int the code
while(j>0)
j=j/2;//log n time

i=2*i // log n time

so O( log n *  log n)=O(log2 n)  

as (log n)k = logk n.

Like, x = log n, then x2 = (log n)2 = log n * log n = log2n.

selected by
1 votes
1 votes
consider n=8

i=1 ...given

j=8

inner while loop j= 4 , 2 , 1   and  i= 2, 4 , 8

outer loop will break nxt time

so, O(logn)
Answer:

Related questions

0 votes
0 votes
1 answer
1
Bikram asked May 26, 2017
485 views
The cost of optimal binary search tree for the identifier set $(a1, a2, a3) =$ (do, if, while) with $p(1) = 0.3, \ p(2) = 0.2, $ $p(3) = 0.15, q (0) = 0.05, q(1) = 0.15...
2 votes
2 votes
2 answers
3
Bikram asked May 26, 2017
366 views
Assume Dijkstra's Algorithm is used to find the shortest paths from node G in the above graph. The total number of edges which are not included in any of the shortest pat...
1 votes
1 votes
2 answers
4
Bikram asked May 26, 2017
484 views
The total number of LCS (Longest Common Subsequences) of $P = abcd123$ and $Q= badc321$ that can be formed are ______.