Binary Search using Java Language
int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x is smaller, ignore right half
if (arr[m] > x)
r = m - 1;
// If x greater, ignore left half
else
l = m + 1;
}
// if we reach here, then element was
// not present
return -1;
}