+ 3
will binary search work when length of array is odd.
to me it seems that it will never reach the last index, plz let me know how it search the last index. https://code.sololearn.com/c7gsuiSEOitM/?ref=app
1 Respuesta
+ 2
unless I'm mistaken, that code doesn't look right. try this. (using c++)
int binarySearch(const int arr[], int target) {
int low = 0;
int high = 2;
int mid;
while(low <= high) {
mid = (low + high) / 2;
if(target == arr[mid])}
return mid;
}
else if(target > arr[mid]) {
low = mid + 1;
}
else {
high = mid - 1; }
}
return -1;
}