Time complexity of algorithm (Big O, Omega)

42 views Asked by At

I'm trying to understand time complexities better and I was hoping someone could help me figure out the time complexity in the worst case for the following algorithm (in pseudocode):

for i= 0 to n−1:
    if A[i] < 0:
        b= 1
        while b < n:
           b=b×2
    end while
  end if
end for
1

There are 1 answers

1
AudioBubble On BEST ANSWER

Hint:

The inner loop is executed Θ(log n) times - when it is executed - because it exits when b has as many bits as n.

Now the worst case happens when all A[i] are negative, so that the inner loop is executed n times.