diff --git a/cpp/include/algorithm/searching/binary_search.hpp b/cpp/include/algorithm/searching/binary_search.hpp index cff1a076..745e3431 100644 --- a/cpp/include/algorithm/searching/binary_search.hpp +++ b/cpp/include/algorithm/searching/binary_search.hpp @@ -30,11 +30,12 @@ template int binary_search(const T& value, const std::vector& sorted_values, const int low, const int high) { + int mid = low + (high - low) / 2; if (value == sorted_values[mid]) return mid; - else if (low <= high) { + else if (low < high) { if (value < sorted_values[mid]) { // value must be between indices low and mid-1, if exists return binary_search(value, sorted_values, low, mid - 1);