0
Code help
Iâm trying to create a method for that does binarysearch for an array. I donâ know whats wrong with my code. I try to test It out but It said Index out of bounds. https://code.sololearn.com/cJZ5l9iee704/?ref=app
3 Answers
+ 1
...
Arrays.sort(a); // sort first
int result = binarysearch(a, 0, n-1, x); // ok
//Arrays.sort(a);
...
// if(r >=1) {
if (r >= l) { // typo R>=L not R>=one
// int mid = l + (r-1)/2;
int mid = l + (r-l)/2; // typo R-L not R-one
...
}
return -1; //ok
}
}
0
in binary search the array needs to be sorted.
swap line 8 and 9, it should be good i think
0
I edited my post for better answer, there was typos about L and One
this new found is similar :
if (r >= l) { // typo R>=L not R>=one
then you dont need adding additional if test in the end, as I was publiced before, only return -1 is enough



