Merge pull request #1 from CodingCookieRookie/myBranch
Handles all corner cases
This commit is contained in:
commit
19caa9723b
21
Searches/Perfect BinarySearch
Normal file
21
Searches/Perfect BinarySearch
Normal 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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user