From cc8c62e2bc2d92efaa44b5833bdf6a3dcfee13f7 Mon Sep 17 00:00:00 2001 From: Satyam Mishra <46562151+satyamm5082000@users.noreply.github.com> Date: Mon, 12 Oct 2020 02:06:48 +0530 Subject: [PATCH] Update binary_search.cpp Made the algo work for a large value of low and high. #hacktoberfest --- Algorithms/searching/C++/binary_search.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Algorithms/searching/C++/binary_search.cpp b/Algorithms/searching/C++/binary_search.cpp index de33bd0..ba1e93f 100644 --- a/Algorithms/searching/C++/binary_search.cpp +++ b/Algorithms/searching/C++/binary_search.cpp @@ -19,7 +19,16 @@ using namespace std; int binarySearch(const int value, const vector& sortedVect, const int low, const int high) { - int mid = (low + high) / 2; +// int mid = (low + high) / 2; + /* Incase the value of low and high is large then + formula (low+high)/2 fails in some case , + therefore it's better to use this formula:- + + int mid = low + (high-low)/2; + + */ + + int mid = low + (high-low)/2; if (value == sortedVect[mid]) return mid;