This version returns the index of the goal, or -1 if not found.
public static int search (String [] arr, int low, int high, String goal) { if ( high < low ) return -1; // goal not found else { int root = (low + high) / 2; int test = goal.compareTo(arr[root]); if ( test == 0 ) return root; // found else if ( test < 0 ) return search(arr, low, root - 1, goal); else return search(arr, root + 1, high, goal); } }
This binary search uses divide-and-conquer and takes O(log(n)) time.
Contents    Page-10    Prev    Next    Page+10    Index