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(final Object[] a, final int fromIndex, final int toIndex, final Object key)
binary Search
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
    int mid = (low + high) >>> 1;
    Comparable midVal = (Comparable) a[mid];
    int cmp = midVal.compareTo(key);
    if (cmp < 0) {
        low = mid + 1;
...
intbinarySearch(int arr[], int low, int high, int key)
binary Search
if (low > high) {
    return -1;
int middle = (low + high) / 2;
if (arr[middle] == key) {
    return middle;
} else if (key > arr[middle]) {
    return binarySearch(arr, middle + 1, high, key);
...
booleanbinarySearch(int codePoint)
Searches if the codepoint is in the range of non-spacing character table.
int max = nonSpacingCharTable.length - 1;
if (codePoint < nonSpacingCharTable[0][0] || codePoint > nonSpacingCharTable[max][1]) {
    return false;
int min = 0;
while (max >= min) {
    int mid = (min + max) / 2;
    if (codePoint > nonSpacingCharTable[mid][1]) {
...
intbinarySearch(int[] array, int key)
binary Search
return privateBinarySearch(array, 0, array.length - 1, key);
intbinarySearch(int[] array, int size, int value)
binary Search
int lo = 0;
int hi = size - 1;
while (lo <= hi) {
    int mid = (lo + hi) >>> 1;
    int midVal = array[mid];
    if (midVal < value) {
        lo = mid + 1;
    } else if (midVal > value) {
...
intbinarySearch(int[] array, int size, int value)
binary Search
int lo = 0;
int hi = size - 1;
while (lo <= hi) {
    int mid = (lo + hi) >>> 1;
    int midVal = array[mid];
    if (midVal < value) {
        lo = mid + 1;
    } else if (midVal > value) {
...
intbinarySearch(int[] index, int key, int begin, int end)
From MTJ Searches for a key in a subset of a sorted array.
end--;
while (begin <= end) {
    int mid = (end + begin) >> 1;
    if (index[mid] < key) {
        begin = mid + 1;
    } else if (index[mid] > key) {
        end = mid - 1;
    } else {
...
intbinarySearch(int[] source, int key)
binary Search
int low = 0, high = source.length - 1, mid;
while (low <= high) {
    mid = (low + high) >>> 1;
    if (key == source[mid]) {
        return mid;
    } else if (key < source[mid]) {
        high = mid - 1;
    } else {
...
intbinarySearch(long[] a, int fromIndex, int toIndex, long key)
Searches a range of the specified array of longs for the specified value using the binary search algorithm.
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
intbinarySearch(long[] a, long key, int endIndex)
binary Search
int low = 0;
int high = endIndex - 1;
while (low <= high) {
    int mid = (low + high) >>> 1;
    long midVal = a[mid];
    if (midVal < key)
        low = mid + 1;
    else if (midVal > key)
...