+ 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
2 odpowiedzi
+ 1
Line 21:
first = mid - 1
should be:
last = mid - 1
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;