// CSE 143, Winter 2012 // 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 = 320000; // length of array to search private static final int SEARCHES = 160000; // number of searches to perform public static void main(String[] args) { System.out.println("Creating the array...\n"); int[] a = createIntArray(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 Strings, // 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. // This works with Strings because String implements the Comparable // interface. public static int binarySearch(String[] a, String target) { return binarySearch(a, target, 0, a.length - 1); } public static int binarySearch(String[] a, String target, int min, int max) { int mid = (min + max) / 2; if (min > max) { return -(min + 1); } else if (a[mid].compareTo(target) == 0) { return mid; } else if (target.compareTo(a[mid]) > 0) { return binarySearch(a, target, mid + 1, max); } else { return binarySearch(a, target, 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(int[] a, int target) { return binarySearch(a, target, 0, a.length - 1); } public static int binarySearch(int[] a, int target, int min, int max) { int mid = (min + max) / 2; if (min > max) { return -(min + 1); } else if (a[mid] == target) { return mid; } else if (target > a[mid]) { return binarySearch(a, target, mid + 1, max); } else { return binarySearch(a, target, 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[] createIntArray(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; } }