in Algorithms retagged by
427 views
1 vote
1 vote

in Algorithms retagged by
427 views

3 Comments

edited by
is it O(n)...??
0
0
But how????
0
0
$\begin{align*} \text{complexity =} \sum_{k=1}^{\log n} \left [ \frac{n}{2^k} \right ] = O(n) \end{align*}$
0
0

1 Answer

2 votes
2 votes
Best answer

Here we have to look at the total iterations that take place to check the running time  - 

The outer i will change as N , N/2 , N/4 ,...

the inner j will loop equal to the value of i.

thus Total iterations = n + n/2 + n/4 +....

Now use geometric mean = a * (1 - r^k) / (1 - r). 

                                     = N * (1 - (1/2) ^ logn ) / (1 - 1/2)  = 2n - 2.                 

 [ k = total no of terms = logn , as N is continuously being divided by 2]

Thus T(n) = O(n)

edited by

Related questions

0 votes
0 votes
1 answer
4