0

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 .

  • We discourage "please check whether my answer is correct" questions, as only "yes/no" answers are possible, which won't help you or future visitors. See here and here. Can you edit your post to ask about a specific conceptual issue you're uncertain about? As a rule of thumb, a good conceptual question should be useful even to someone who isn't looking at the problem you happen to be working on. If you just need someone to check your work, you might seek out a friend, classmate, or teacher. – D.W. May 02 '23 at 20:25
  • The outer 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

0 Answers0