This binary search code give always -1 output. I don’t know what is problem in this however it is same code that i learned in online java crux course??
public class Binary {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr2= {2,8,454,5,89,465,464,15,546,79};
int items=5;
System.out.println(binarySearch(arr2, items));
}
public static int binarySearch(int[] arr, int item) {
int lo = 0;
int hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] < item) {
lo = mid + 1;
} else if (arr[mid] > item) {
hi = mid - 1;
} else {
return mid;
}
}
return -1;
}
}