+ 1

I need help getting my binary search algorithm to work

currently I'm trying to write up a binary search program that I originally wrote in c++, but the algorithm can only find the desired value if it's in the middle. can someone help? https://code.sololearn.com/caadu0d6CY86/?ref=app

4th Sep 2017, 5:32 PM
X-1
X-1 - avatar
2 odpowiedzi
+ 1
Line 21: first = mid - 1 should be: last = mid - 1
4th Sep 2017, 5:41 PM
Dennis
Dennis - avatar
0
Hye, X-1, I don't know C++, I may answer your question by java, at Line 5 i think your method's input is "arr",but you sorted myArr.Should it be Arr.sort()? Another thing you need to pay attention is how to find the mid index, as you write is okay for this question, but what will happened if this array was big enough? it will be overflow.So the correct way to find mid index is: mid = left + (right - left)/2;
5th Sep 2017, 12:09 AM
Ran
Ran - avatar