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(byte[] a, byte key, int startPos)
binary Search
if (startPos > a.length || startPos < 0) {
    return -1;
for (int i = startPos; i < a.length; i++) {
    if (a[i] == key) {
        return i;
return -1;
intbinarySearch(byte[] a, int key)
Search for a big-endian 4-byte integer in a array of bytes.
int low = 0;
int high = a.length;
while (low < high) {
    int mid = (low + high) >>> 1;
    if (mid % 4 != 0) {
        if (high == a.length) {
            mid = low;
        } else {
...
intbinarySearch(byte[] readBuffer, int offset, int length, byte value)
binary Search
for (int i = offset; i < length; i++) {
    if (readBuffer[i] == value)
        return i;
return -1;
intbinarySearch(char[] arr, int key)
binary Search
int length = arr.length;
if (key <= arr[length - 1]) {
    int l = 0;
    int r = length - 1;
    while (l <= r) {
        int mid = (l + r) >>> 1;
        char val = arr[mid];
        if (key > val) {
...
intbinarySearch(double experience, int min, int max)
Utilizes a basic binary search algorithm to find the level for the specified experience.
int mid = (min + max) / 2;
double value = EXPERIENCE_TABLE[mid];
if (value > experience) {
    return binarySearch(experience, min, mid - 1);
} else if (value == experience || EXPERIENCE_TABLE[mid + 1] > experience) {
    return mid + 1;
} else {
    return binarySearch(experience, mid + 1, max);
...
intbinarySearch(double values[], double search)
binary Search
int min = 0, max = values.length;
int index = max / 2;
while (max - min > 1) {
    if (values[index] == search) {
        return index;
    if (values[index] < search) {
        min = index;
...
intbinarySearch(double[] a, double key)
Perform a binary search on a sorted array a to find the element with the nearest element to key .
int l = 0;
int h = a.length - 1;
int i;
do {
    i = (int) (((long) l + (long) h) / 2L);
    if (key > a[i]) {
        l = i + 1;
    } else if (key < a[i]) {
...
intbinarySearch(double[] arr, double value, int p, int q)
Add by Chuan binary search
if (p > q)
    return p - 1;
int r = (p + q) / 2;
if (arr[r] == value)
    return r;
else if (arr[r] < value)
    return binarySearch(arr, value, r + 1, q);
else
...
intbinarySearch(final double[] array, final double val)
binary Search
int low = 0;
int high = array.length - 1;
final boolean keyIsNaN = Double.isNaN(val);
while (low <= high) {
    final int mid = (low + high) >> 1;
    final double midVal = array[mid];
    if (midVal < val) { 
        low = mid + 1;
...
intbinarySearch(final Enum[] full, final int[] partial, final char key, final int index)
binary Search
int low = 0;
int high = partial.length - 1;
while (low <= high) {
    int mid = (low + high) >>> 1;
    final String name = full[partial[mid]].toString();
    char midVal = index < name.length() ? name.charAt(index) : ' ';
    if (midVal < key)
        low = mid + 1;
...