public class BinarySearch { public static void main (String[] argv) { // Sorted array: int[] testData = {24, 32, 41, 42, 51, 63, 71, 73, 85, 87}; // Create a test value to search for. int searchTerm = 32; // Note: initially, the "range" is 0,...,testData.length-1 boolean found = binarySearch (testData, searchTerm, 0, testData.length-1); // Print result. System.out.println ("found = " + found); // Another search. searchTerm = 55; found = binarySearch (testData, searchTerm, 0, testData.length-1); System.out.println ("found = " + found); } static boolean binarySearch (int[] A, int value, int start, int end) { // Bottom-out cases: if (start == end) { // There's only one value to check: A[start]=value? if (A[start] == value) { return true; } else { return false; } } // If there are only two values in the range: if (end == start+1) { if (A[start] == value) { return true; } else if (A[end] == value) { return true; } else { return false; } } // Otherwise recurse. We know that A[start] <= value <= A[end]. // Find the middle: int mid = (start + end) / 2; if (value <= A[mid]) { // Search the left half: A[start],...,A[mid] return binarySearch (A, value, start, mid); } else { // Search the right half: A[mid+1],...,A[end] return binarySearch (A, value, mid+1, end); } } }