I have the following algorithm which takes as an input a non negative integer n :
i = n
while i > 0 do :
$\,$ $\,$ $\,$ $\,$i = i - 1
$\,$ $\,$ $\,$ $\,$j = 1
$\,$ $\,$ $\,$ $\,$ $\,$ $\,$ $\,$ $\,$while j $\le$ n do
$\,$ $\,$ $\,$ $\,$ $\,$ $\,$ $\,$ $\,$ $\,$ $\,$ $\,$ j = 2j
The outer while loop has complexity of O(n) and the inner loop has complexity of O(logn) . So i assume the complexity of the algorithm as a whole is O(nlogn) .
Is this correct ? Thank you .
while
loop cannot be said to have complexity $O(n)$, because its body does not run in constant time. Rather say that it is executed $n$ times. – May 03 '23 at 07:33