Java Binary Search binarySearch(long[] array, long key)

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

Description

binary Search

License

Apache License

Declaration

public static final int binarySearch(long[] array, long key) 

Method Source Code

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

public class Main {
    public static final int binarySearch(long[] array, long key) {
        if (array.length == 0)
            return -1;
        int min = 0, max = array.length - 1;
        long minVal = array[min], maxVal = array[max];
        int nPreviousSteps = 0;

        for (;;) {
            if (key <= minVal)
                return key == minVal ? min : -1 - min;
            if (key >= maxVal)
                return key == maxVal ? max : -2 - max;

            if (min == max)
                return -1;
            int pivot;
            // A typical binarySearch algorithm uses pivot = (min + max) / 2.
            // The pivot we use here tries to be smarter and to choose a pivot close to the expectable location of the key.
            // This reduces dramatically the number of steps needed to get to the key.
            // However, it does not work well with a logaritmic distribution of values, for instance.
            // When the key is not found quickly the smart way, we switch to the standard pivot.
            if (nPreviousSteps > 2) {
                pivot = (min + max) >> 1;
                // stop increasing nPreviousSteps from now on
            } else {
                // NOTE: We cannot do the following operations in int precision, because there might be overflows.
                //       long operations are slower than float operations with the hardware this was tested on (intel core duo 2, JVM 1.6.0).
                //       Overall, using float proved to be the safest and fastest approach.
                pivot = min + (int) ((key - (float) minVal) / (maxVal - (float) minVal) * (max - min));
                nPreviousSteps++;/*from  w  ww.  j  av a 2  s .c o  m*/
            }

            long pivotVal = array[pivot];

            // NOTE: do not store key - pivotVal because of overflows
            if (key > pivotVal) {
                min = pivot + 1;
                max--;
            } else if (key == pivotVal) {
                return pivot;
            } else {
                min++;
                max = pivot - 1;
            }
            maxVal = array[max];
            minVal = array[min];
        }
    }
}

Related

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