Time complexity of an algorithm - n or n*n?

Viewed 259

I'm trying to find out which is the Theta complexity of this algorithm. (a is a list of integers)

def sttr(a):
    for i in xrange(0,len(a)):
        while s!=[] and a[i]>=a[s[-1]]:
            s.pop()
        s.append(i)
    return s

On the one hand, I can say that append is being executed n (length of a array) times, so pop too and the last thing I should consider is the while condition which could be executed probably 2n times at most.

From this I can say that this algorithm is at most 4*n so it is THETA(n).

But isn't it amortised analysis?

On the other hand I can say this:

There are 2 nested cycles. The for cycle is being executed exactly n times. The while cycle could be executed at most n times since I have to remove item in each iteration. So the complexity is THETA(n*n).

I want to compute THETA but don't know which of these two options is correct. Could you give me advice?

1 Answers
Related