The n
in O(n)
means precisely the input size. So, if I have this code:
def findmax(l):
maybemax = 0
for i in l:
if i > maybemax:
maybemax = i
return maybemax
Then I'd say that the complexity is O(n)
-- how long it takes is proportional to the input size (since the loop loops as many times as the length of l
).
If I had
def allbigger(l, m):
for el in l:
for el2 in m:
if el < el2:
return False
return True
then, in the worst case (that is, when I return True
), I have one loop of length len(l)
and inside it, one of length len(m)
, so I say that it's O(l * m)
or O(n^2)
if the lists are expected to be about the same length.