Skip to content

Commit 4fcad4f

Browse files
Handles all corner cases
1 parent c8ef1fd commit 4fcad4f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Searches/Perfect BinarySearch

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
static int binarySearch(int[] arr, int target) {
2+
int low = 0 ;
3+
int high = arr.length - 1 ;
4+
5+
while(low <= high) {
6+
int mid =(low + high) / 2;
7+
8+
if(arr[mid] == target) {
9+
return mid;
10+
}
11+
else if(arr[mid] > target) {
12+
high = mid - 1;
13+
}
14+
else {
15+
low = mid + 1;
16+
}
17+
18+
}
19+
return -1;
20+
}
21+

0 commit comments

Comments
 (0)