Java binary search (and get index if not there) review
2 minor things:


Remove the unnecessary comparison. If

sortedArray[mid].equals(target)


fails, it will again compare the two strings in the next if condition. Instead, you can just do:

int c = target.compareTo(sortedArray[mid]);
if(c == 0)
  return mid;
else if(c &lt; 0)
  ...

To keep the method signature simple, you can overload it and delegate to the one that does the work:

public int binarySearch(String[] sortedArray, String target) {
  binarySearch(sortedArray, target, 0, sortedArray.length - 1);
}