public class BinarySearch3 { public static void main (String[] argv) { // Fill an array with some random values - for testing. 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); // Print result. System.out.println ("found = " + found); // Another search. searchTerm = 55; found = binarySearch (testData, searchTerm); System.out.println ("found = " + found); } static boolean binarySearch (int[] A, int value) { // Instead of calling the method recursively, we can also // set start and end initially, and simply adjust the values // in a loop. int start = 0; int end = A.length - 1; while (start <= end) { int mid = (start + end) / 2; if (value == A[mid]) { return true; } else if (value < A[mid]) { end = mid-1; } else { start = mid+1; } } // end-while return false; } }