closed by
24,443 views

4 Answers

Best answer
2 votes
2 votes
i =n   n/2      n/4    n/8  ....................logn times
j=n     n/2     n/4    n/8 ...........logn tmes

T(n) = n+ n/2 + n/4 + n/8 + ...........logn time
       = n(1+ 1/2 + 1/4 + .............logn times) Decreasing GP
         =O(n)
selected by
3 votes
3 votes

Time Complexity of the above algorithm $=$No of times " $count+=1$ " executes.Following Table gives better picture$\Rightarrow$

 

i j
$N$ $N$
$N/2$ $N/2$
$N/4$ $N/4$
$\vdots$ $\vdots$
$N/N$ $N/N$



$\Rightarrow \, T\left ( N \right )=\left ( N/1+N/2+N/4 \cdots N/N \right )$

                             =$N\left ( 1/1+1/2+1/4 \cdots 1/N \right )$

    $\left ( 1/1+1/2+1/4+\cdots 1/N \right ) =\sum_{k=0}^{k=n} 1/2^{k}$

solving it ,we can say that  $\sum_{k=0}^{k=n} 1/2^{k} < < < \sum_{k=0}^{k=\infty } 1/2^{k}$

$\sum_{k=0}^{k=\infty } 1/2^{k}$=$1/\left ( 1-\left ( 1/2 \right ) \right )=2$

$\therefore \sum_{k=0}^{k=n } 1/2^{k}  \approx \Theta \left ( k \right )$                              

$T\left ( N \right )=\Theta \left ( N*\left ( \Theta \left ( k \right ) \right ) \right )$

$T\left ( N \right )=\Theta \left( N \right )$

edited by
1 votes
1 votes

Here lets take eg :

LET n = 10

initially: i  = 10 (first loop)

               j = 0 < 10(i) so it will loop from 0 to 9 times

NOW AFTER NESTED LOOP GETS OVER THIS TAKES PLACE

  i /= 2

SO value of i = 5 (first loop ) 2 iteration.

this time j will run from     j = 0 < 5(i) so it will loop from 0 to 5 times

each time value of i will be divided by 2 and similarly for corresponding value of j will iterate from 0 to i/2 times.

so, T(n) = O(n + n/2 + n/4 + … 1) = O(n) for j (This is for iteration of j only )

i               j

10           0-9 times

5              0 - 4 times

2             0 - 1 times

similarly value of j which was initially n i.e 10  gets decreased in order on n/2 forming GP & thus we get O(n)

Related questions

0 votes
0 votes
0 answers
2
0 votes
0 votes
1 answer
3
NeelParekh asked Jul 27, 2023
279 views
If an array is split in the form of increasing and decreasing order then what is TC to find minimum element in the array?
2 votes
2 votes
1 answer
4
h4kr asked Dec 30, 2022
470 views
What is the correct time complexity in $\theta()$ ?