Java Binary Search binarySearch(int[] array, int size, int value)

Here you can find the source of binarySearch(int[] array, int size, int value)

Description

binary Search

License

Apache License

Declaration

static int binarySearch(int[] array, int size, int value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    static int binarySearch(int[] array, int size, int value) {
        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;/*w  w w  .ja  v a  2  s .c om*/
            } else if (midVal > value) {
                hi = mid - 1;
            } else {
                return mid; // value found
            }
        }
        return ~lo; // value not present
    }
}

Related

  1. binarySearch(final Enum[] full, final int[] partial, final char key, final int index)
  2. binarySearch(final Object[] a, final int fromIndex, final int toIndex, final Object key)
  3. binarySearch(int arr[], int low, int high, int key)
  4. binarySearch(int codePoint)
  5. binarySearch(int[] array, int key)
  6. binarySearch(int[] array, int size, int value)
  7. binarySearch(int[] index, int key, int begin, int end)
  8. binarySearch(int[] source, int key)
  9. binarySearch(long[] a, int fromIndex, int toIndex, long key)