Skip to content

Commit cc8c62e

Browse files
Update binary_search.cpp
Made the algo work for a large value of low and high. #hacktoberfest
1 parent b2f1a2f commit cc8c62e

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Algorithms/searching/C++/binary_search.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@
1919
using namespace std;
2020

2121
int binarySearch(const int value, const vector<int>& sortedVect, const int low, const int high) {
22-
int mid = (low + high) / 2;
22+
// int mid = (low + high) / 2;
23+
/* Incase the value of low and high is large then
24+
formula (low+high)/2 fails in some case ,
25+
therefore it's better to use this formula:-
26+
27+
int mid = low + (high-low)/2;
28+
29+
*/
30+
31+
int mid = low + (high-low)/2;
2332

2433
if (value == sortedVect[mid])
2534
return mid;

0 commit comments

Comments
 (0)