Skip to content

Instantly share code, notes, and snippets.

@fge
Created January 23, 2015 22:10
Show Gist options
  • Select an option

  • Save fge/d305c943680ff8b0d290 to your computer and use it in GitHub Desktop.

Select an option

Save fge/d305c943680ff8b0d290 to your computer and use it in GitHub Desktop.
private int binarySearch(final int index)
{
return doBinarySearch(0, nrLines - 1, index);
}
private int doBinarySearch(final int low, final int high, final int index)
{
// Guaranteed to always succeed at this point
if (low == high)
return low;
final int middle = (low + high) / 2;
final Range<Integer> range = lines.get(middle);
if (range.contains(index))
return middle;
return index < range.lowerEndpoint()
? doBinarySearch(low, middle, index)
: doBinarySearch(middle, high, index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment