Suppose an algorithm goes through a list of n integers and for every iteration of the loop it is needs to check if the current evaluated element of the list is even. If it is even, return the index of the integer that is evaluated as even.
How come the algorithm would have 2n+1 comparison?
I thought linear search would have n comparision because it is going through n elements. +1 comparison for the if statement. So that would make the algorithm O(n+1) comparison, no?. Where did the extra n come from?
procedure last_even_loc(a1,a2,...,an:integers); location = 0; for i = 1 to n if (a_i = 0) (mod 2) then location = i return location;