Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T> int BinarySearch(vector<T>& vec, T& request) {
- T low = 0;
- T high = vec.size() - 1;
- while (low < high) {
- T mid = (low / 2 ) + (high / 2); // Styled this way to avoid overflows.
- // This looks like where the bug happens, basically low and high both
- // become 93 while mid becomes 92,
- // it then exits the loop and returns -1 because low is not lower than
- // high anymore.
- if (vec[mid] == request) {
- return mid;
- }
- else if (vec[mid] < request) {
- low = mid + 1;
- }
- else if (vec[mid] > request) {
- high = mid - 1;
- }
- }
- return - 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement