Is this a reasonable binary search implementation? import java.util.Arrays;

public class BinarySearch {

  // this class should not be instantiated

  private BinarySearch() { }

  // searches for the integer key in the sorted array a[]
  // @param key the search key
  // @param a the array of integers, must be sorted in ascending order
  // @return index of key in array a[] if present; -1 if not present

  public static int rank(int key, int[] a) {
    int lo = 0;
    int hi = a.length - 1;
    while (lo &lt;= hi) {
      // key is in a[lo..hi] or not present
      int mid = lo + (hi - lo) / 2;
      if (key &lt; a[mid]) hi = mid - 1;
      else if (key &gt; a[mid]) lo = mid + 1;
      else return mid;
    }
    return 1;

  }

  // reads in a sequence of integers from the whitelist file, specified as a command line argument. reads in integers from standard input and prints to standard output
  // those integers that do not appear in the file.
  public static void main(String[] args){

    // read the integers from a file
    In in = new In(args[0]);
    int[] whitelist = in.readAllInts();

    // sort the array
    Arrays.sort(whitelist);

    // read integer key from standard input; print if not in whitelist
    while (!StdIn.isEmpty()) {
      int key = StdIn.readInt();
      if (rank(key, whitelist) == -1)
        StdOut.println(key);
    }
  }

}
Your implentation can search integers, we expect to search in an array of any comparable objects.

I think you have a bug if the element is not found. It will return 1 instead of -1 /  throwing an exception. Add unit tests.

Also, Java is now open source, you can compare your implementation with the one from the JDK (Arrays.binarySearch).

Also, in code style:


it is recommended to use {} in if block, even if they are one-liner