class BinarySearchApp
{
int binarySearch(int arr[], int l, int r, int x)
{
if (r>=l)
{
int mid = l + (r - l)/2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
public static void main(String args[])
{
BinarySearchApp obj = new BinarySearchApp();
int arr[] = {3,5,9,10,20};
int n = arr.length;
int searchKey = 9;
System.out.println("Array Elements:");
for(int i : arr) System.out.print(i + " ");
System.out.println();
System.out.println("Searching for " + searchKey);
int result = obj.binarySearch(arr,0,n-1,searchKey);
if (result == -1)
System.out.println("Element not found");
else
System.out.println("Element found at index " + result);
}
}