im writing a binary search method for a university assignment, and although i feel like this is the correct way of doing it, I feel like the run time is longer then it should be.. does anyone see any mistakes in this? iterator
is a custom class, nothing fancy, does what you would expect it to. vec
is a vector of iterators, that point to a bigger linked list of what im "really" searching for
iterator searchVec(const I& item)
{
int left = 0;
int right = (int)vec.size()-1;
int mid = (right+left)/2;
while(*vec.at(left) != *vec.at(right)){
mid = (right+left)/2;
if (mid == 0 || mid == vec.size()-1){
//nothign else to search, we didnt find anything
return *vec.end();
}
if (*vec.at(mid) == item){
return vec.at(mid);
}
else if (item > *vec.at(mid)){
left = mid;
}
else if (item < *vec.at(mid)){
right = mid;
}
}
return vec.at(mid);
}