22 lines
445 B
Plaintext
22 lines
445 B
Plaintext
static int binarySearch(int[] arr, int target) {
|
|
int low = 0 ;
|
|
int high = arr.length - 1 ;
|
|
|
|
while(low <= high) {
|
|
int mid =(low + high) / 2;
|
|
|
|
if(arr[mid] == target) {
|
|
return mid;
|
|
}
|
|
else if(arr[mid] > target) {
|
|
high = mid - 1;
|
|
}
|
|
else {
|
|
low = mid + 1;
|
|
}
|
|
|
|
}
|
|
return -1;
|
|
}
|
|
|