Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update binary_search.cpp
Made the algo work for a large value of low and high.
#hacktoberfest
  • Loading branch information
satyamm5082000 authored Oct 11, 2020
commit cc8c62e2bc2d92efaa44b5833bdf6a3dcfee13f7
11 changes: 10 additions & 1 deletion Algorithms/searching/C++/binary_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@
using namespace std;

int binarySearch(const int value, const vector<int>& 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;
Expand Down