//recursive binary search on ints

public class BinSearchAlt {

    // Alternative version- only check for value when you
    // are down to one element.
    // Helps improve performance in worst case, hinders performance in best case! 
    public static int recBinarySearch(int a[], int value, int low, int high) {
	if (low == high) {
	    // only check == when there is only one element left.
	    if (a[low] == value) {
		return low;
	    } else {
		return -1;
	    }
	} else {
	    int mid = (low + high) / 2;
	    if (a[mid] < value) {
		// recurse on upper half
		return recBinarySearch(a, value, mid + 1, high);
	    } else {
		// recurse on bottom half (note that we include mid this time)
		return recBinarySearch(a, value, low, mid);
	    }
	}
    }

    public static void main(String args[]) {
	int[] array = {0, 1, 3, 4, 5, 8, 9, 11};
	System.out.println(recBinarySearch(array, 4, 0, array.length-1));

    }

}
