Reputation: 35
Does it have to compare every element one by one?
In such case it must be O(n) right?
Upvotes: 0
Views: 2934
Reputation: 495
when you want to compare two vectors.
Generally, in our program, the size of two vectors should already be given (not a variable as the input size), in that situation, the comparison between two vectors is O(1) since it must finish the comparison in a fixed time.
Upvotes: -1
Reputation: 490438
It doesn't necessarily compare all the elements.
If the two vectors are of different lengths, it can return false based only on the difference in length, in constant time.
Otherwise, yes, it has to compare elements until it encounters a difference (all the elements if the two are equal).
Upvotes: 4