Reputation: 71
I was just wondering when understanding the time complexity of an algorithm like the one below.
For a python list, if we have a for loop iterating over it, and then a containment check, would the time complexity of that be O(n^2).
I know both are O(n) (or I think) so having them nested in one another would that make it O(n^2)?
I think if this "list" is actually a list, then the time complexity of the code below is O(n^2). But if it's a dictionary it would be O(n) because lookup is O(1). Is that correct?
Thanks for any help in advance!
for element in list:
if x in list:
Upvotes: 6
Views: 2823
Reputation: 361645
Your analysis is correct.
Upvotes: 4