Java implementation of Binary Search Algorithm

https://‮al.www‬utturi.com
Java implementation of Binary Search Algorithm

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;
}
Created Time:2017-09-01 18:44:40  Author:lautturi