Search code examples
timetime-complexitybig-ogomega

Time complexity of algorithm (Big O, Omega)


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

Solution

  • 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.