Merge pull request #1 from CodingCookieRookie/myBranch

Handles all corner cases
This commit is contained in:
CodingCookieRookie 2020-05-19 23:26:51 +08:00 committed by GitHub
commit 19caa9723b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,21 @@
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;
}