Write a program for binary search in java?
package javatute; public class BinarySearch { public int binarySearchMethod(int ary[], int key) { int l = 0, r = ary.length - 1; while (l <= r) { int mid = l + (r - l) / 2; if (ary[mid] == key) { return mid; } if (ary[mid] > key) { r = mid - 1; } else { l = mid + 1; } } return -1; } public static void main(String[] args) { BinarySearch nb = new BinarySearch(); int a[] = { 12, 15, 18, 20, 35, 40, 45 }; int result = nb.binarySearchMethod(a, 18); System.out.println(result); } }