public class BinarySearch { public static void main(String[] args) { int[] a = {1,2,3,4,5,6,7,8,9}; int binarySerach = binarySerach(a, 4); System.out.println(binarySerach); } public static int binarySerach(int[] a, int key) { int low = 0; int high = a.length - 1; int mid = -1; while (low <= high) { mid = (low + high) / 2; if (key > a[mid]) { low = mid + 1; } else if (key < a[mid]) { high = mid - 1; } else { return mid; } } return mid; } }