package lab12;

public class BinarySearch {

    private static boolean binarySearch(int[] myList, int numberToFind) {
        return binarySearch(myList, 0, myList.length - 1, numberToFind);
    }

    private static boolean binarySearch(int[] arr, int lo, int hi, int target) {
        if (lo > hi) return false;

        final int mid = (hi - lo) / 2;
        return arr[mid] == target || (arr[mid] < target
				? binarySearch(arr, mid + 1, hi, target)
                : binarySearch(arr, lo, mid - 1, target));
    }

    public static void main(String[] args) {
        // Create an array of sorted numbers

        int[] evenList =
        {   2,  4,  9, 11, 17, 19, 22, 29, 30, 33,
            39, 43, 46, 47, 51, 52, 54, 56, 58, 59,
            63, 69, 70, 79, 88, 89, 92, 96, 98, 99
        };

        // Can we find every number?
        for (int i = evenList.length - 1; i >= 0; i--) {
            if (binarySearch(evenList, evenList[i]))
                System.out.printf("%d was found.\n\n", evenList[i]);
            else
                System.out.printf("%d was not found.\n\n", evenList[i]);
        }

        // Will we not find these numbers?
        int[] testCases = { 1, 44, 100, 32 };

        for (int i = 0; i < testCases.length; i++) {
            if (binarySearch(evenList, testCases[i]))
                System.out.printf("%d was found.\n\n", testCases[i]);
            else
                System.out.printf("%d was not found.\n\n", testCases[i]);
        }
    }

}



