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

19th Jul 2019, 5:03 AM
Beanie
Beanie - avatar
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 } }
19th Jul 2019, 7:31 PM
zemiak
0
in binary search the array needs to be sorted. swap line 8 and 9, it should be good i think
19th Jul 2019, 5:16 AM
Taste
Taste - avatar
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
20th Jul 2019, 8:32 AM
zemiak