Java Utililty Methods Binary Search

List of utility methods to do Binary Search

Description

The list of methods to do Binary Search are organized into topic(s).

Method

intbinarySearch(long[] array, long key)
binary Search
if (array.length == 0)
    return -1;
int min = 0, max = array.length - 1;
long minVal = array[min], maxVal = array[max];
int nPreviousSteps = 0;
for (;;) {
    if (key <= minVal)
        return key == minVal ? min : -1 - min;
...
intbinarySearch(long[] data, long key, int low, int high)
binary Search
if (data == null) {
    throw new IllegalArgumentException("null array");
if (data.length == 0) {
    return -1;
if (low <= high && (low < 0 || high < 0)) {
    throw new IllegalArgumentException("can't search negative indices");
...
intbinarySearch(Object[] a, Object key)
binary Search
int x1 = 0;
int x2 = a.length;
int i = x2 / 2;
while (x1 < x2) {
    int c = ((Comparable) a[i]).compareTo(key);
    if (c == 0) {
        return i;
    if (c < 0) {
        x1 = i + 1;
    else {
        x2 = i;
    i = x1 + (x2 - x1) / 2;
return -1 * i;
intbinarySearch(String[] arr, String value)
binary Search
int left = 0;
int right = arr.length;
int index = -1;
while (left < right) {
    int m = (left + right) / 2;
    int res = arr[m].compareTo(value);
    if (res == 0) {
        index = m;
...
booleanbinarySearch(String[] array, String sought)
binary Search
int first = 0;
int len = array.length;
int half, middle;
int compare;
while (len > 0) {
    half = len / 2;
    middle = first + half;
    compare = array[middle].compareTo(sought);
...
intbinarySearch(T[] a, T x)
binary Search
int low = 0, high = a.length - 1;
while (low <= high) {
    int mid = (low + high) / 2;
    if (a[mid].compareTo(x) < 0)
        low = mid + 1;
    else if (a[mid].compareTo(x) > 0)
        high = mid - 1;
    else
...
intbinarySearch0(float[] a, int fromIndex, int toIndex, float key)
binary Search
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
    int mid = (low + high) >>> 1;
    float midVal = a[mid];
    int cmp;
    if (midVal < key) {
        cmp = -1; 
...
intbinarySearchCeil(double[] a, double key)
Perform a binary search on a sorted array a to find the element with the smallest distance to key .
if (a.length == 0) {
    return -1;
int i = binarySearch(a, key);
if (i >= 0 && a[i] < key) {
    i++;
return i;
...
intbinarySearchIndex(double[] values, double toFind)
Find the index i such that values[i]<=toFindint n = values.length; int left = -1; int right = n; while (right > left + 1) { int middle = (right + left) / 2; if (toFind < values[middle]) { right = middle; } else { ...
intbinarySearchLower(int[] theSearchBase, int theLowerBound, int theUpperBound, int theKey)
An extension of the standard binary search that returns the next lowest Integer in an array if the passed key does not exist in the array.
if (theSearchBase == null) {
    throw new IllegalArgumentException("Can not operate on a null search base.");
} else if (theSearchBase.length < 2) {
    throw new IllegalArgumentException("Can not operate on a search base with a length of less than 2.");
} else if (theLowerBound > theUpperBound) {
    throw new IllegalArgumentException(
            "Can not operate when the lower bound is greater than the lower bound.");
} else if (theLowerBound == theUpperBound) {
...