// CSE 143, Winter 2011, Marty Stepp // This program compares the runtime of sequential and binary search. import java.util.*; // for Random public class Searching { private static final Random RAND = new Random(); // global random generator private static final int LENGTH = 8000000; // length of array to search private static final int SEARCHES = 16000000; // number of searches to perform public static void main(String[] args) { System.out.println("Creating the array...\n"); int[] a = createArray(LENGTH); System.out.println("Performing searches...\n"); long startTime = System.currentTimeMillis(); // perform many searches and time how long it takes for (int i = 1; i <= SEARCHES; i++) { int index = binarySearch(a, 42); if (index >= 0 && a[index] != 42) { throw new RuntimeException("search returned an incorrect answer"); } } long endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; System.out.println(elapsedTime + " ms to perform " + SEARCHES + " searches over " + LENGTH + " elements"); } // Returns the index of the given value in the given array of integers, // or a negative number if the value is not found in the array. // Precondition: The array must be sorted. If the array is not sorted, // the result is undefined. public static int binarySearch(int[] a, int value) { return binarySearch(a, value, 0, a.length - 1); } private static int binarySearch(int[] a, int value, int min, int max) { if (min > max) { // base case: not found return -1; } else { int mid = (min + max) / 2; if (a[mid] == value) { // base case: found the value return mid; } else if (a[mid] < value) { // middle element is too small; go right return binarySearch(a, value, mid + 1, max); } else { // middle element is too big; go left return binarySearch(a, value, min, mid - 1); } } } // Returns the index of the given value in the given array of integers, // or a negative number if the value is not found in the array. // Precondition: The array must be sorted. If the array is not sorted, // the result is undefined. public static int binarySearch(String[] a, String value) { return binarySearch(a, value, 0, a.length - 1); } private static int binarySearch(String[] a, String value, int min, int max) { if (min > max) { // base case: not found return -1; } else { int mid = (min + max) / 2; if (a[mid].equals(value)) { // base case: found the value return mid; } else if (a[mid].compareTo(value) < 0) { // middle element is too small; go right return binarySearch(a, value, mid + 1, max); } else { // middle element is too big; go left return binarySearch(a, value, min, mid - 1); } } } // Returns the index of the given value in the given array of integers, // or -1 if the value is not found in the array. // Throws a NullPointerException if a is null. public static int sequentialSearch(int[] a, int value) { for (int i = 0; i < a.length; i++) { if (a[i] == value) { return i; } } return -1; } // Creates an array of the given length, fills it with random integers, // arranges it into sorted order, and returns it. public static int[] createArray(int length) { int[] a = new int[length]; for (int i = 0; i < a.length; i++) { a[i] = RAND.nextInt(1000000000); } Arrays.sort(a); return a; } // Creates an array of the given length, fills it with random strings, // arranges it into sorted order, and returns it. public static String[] createStringArray(int length) { String[] a = new String[length]; for (int i = 0; i < a.length; i++) { int strLen = RAND.nextInt(10); // strings of up to 10 letters in length String s = ""; for (int j = 0; j < strLen; j++) { s += (char) (RAND.nextInt(26) + 'a'); } a[i] = s; } Arrays.sort(a); return a; } }