How Can I Figure The Order Of Complexity?
I think I know the complexity of these 2 codes but I simply cant find the right equations to prove it. The first one I assume is O(loglogn). The second one is O(n^2). def f1(lst):
Solution 1:
I think you can try to get the recursive equation first, and then use master theorem or something else to solve the recursive equations. For the first one, we use the length of the lst as the parameter.
deff1(lst1): #len(lst2)=N
i=2while i<len(lst1):
print(lst1[i])
i **= 2deff1(lst2): #len(lst2)=N^2
i=2while i<len(lst2):
print(lst2[i])
i **= 2
you will notice that the second one will execute only once more than the first one. so you get
T(N^2)=T(N)+1.
For simplification, just suppose N^2=2^k,
then T(2^k)=T(2^(k/2))+1,let f(k)=T(2^k),
then f(k)=f(k/2)+1,
with the master theorem, T(N^2) = f(k) = log(k) = log(log(2^k)) = log(log(N^2)).
we get T(N) = O(loglog(N)) at last.
Post a Comment for "How Can I Figure The Order Of Complexity?"