Site Search:

BinarySearch




   int binarySearch(int Key, int[] arr) {
       //arr must be sorted
       int i = 0, j = arr.length - 1, mid = 0;
       while(i <= j) {
            mid = i + (j - i)/2;
            if(key > arr[mid]) i = mid + 1;
            if(key < arr[mid]) j = mid - 1;
            else return mid;
       }
       return -1;
    }