Java Binary Search binarySearch(long[] a, long key, int endIndex)

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

Description

binary Search

License

Apache License

Parameter

Parameter Description
a the array to search in
key the key to search for
endIndex the limit for the search

Return

the index of the key to search for

Declaration

public static int binarySearch(long[] a, long key, int endIndex) 

Method Source Code

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

public class Main {
    /**// w  w w . j a v a  2 s .  c o  m
     * @param a
     *            the array to search in
     * @param key
     *            the key to search for
     * @param endIndex
     *            the limit for the search
     * @return the index of the key to search for
     */
    public static int binarySearch(long[] a, long key, int endIndex) {
        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)
                high = mid - 1;
            else
                return mid;
        }
        return -(low + 1);
    }
}

Related

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