Java Binary Search binarySearch(int[] source, int key)

Here you can find the source of binarySearch(int[] source, int key)

Description

binary Search

License

LGPL

Declaration

public static int binarySearch(int[] source, int key) 

Method Source Code

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

public class Main {

    public static int binarySearch(int[] source, int key) {
        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;/*from  www .  j a  v  a  2  s .c  o m*/
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }
}

Related

  1. binarySearch(int codePoint)
  2. binarySearch(int[] array, int key)
  3. binarySearch(int[] array, int size, int value)
  4. binarySearch(int[] array, int size, int value)
  5. binarySearch(int[] index, int key, int begin, int end)
  6. binarySearch(long[] a, int fromIndex, int toIndex, long key)
  7. binarySearch(long[] a, long key, int endIndex)
  8. binarySearch(long[] array, long key)
  9. binarySearch(long[] data, long key, int low, int high)