Java Binary Search binarySearch(Object[] a, Object key)

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

Description

binary Search

License

Open Source License

Declaration

public static int binarySearch(Object[] a, Object key)
        

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static int binarySearch(Object[] a, Object key)
    /*      */{/*w w w.  ja  v a 2 s.c  om*/
        /* 3669 */int x1 = 0;
        /* 3670 */int x2 = a.length;
        /* 3671 */int i = x2 / 2;
        /* 3672 */while (x1 < x2) {
            /* 3673 */int c = ((Comparable) a[i]).compareTo(key);
            /* 3674 */if (c == 0) {
                /* 3675 */return i;
                /*      */}
            /* 3677 */if (c < 0) {
                /* 3678 */x1 = i + 1;
                /*      */}
            /*      */else {
                /* 3681 */x2 = i;
                /*      */}
            /* 3683 */i = x1 + (x2 - x1) / 2;
            /*      */}
        /* 3685 */return -1 * i;
        /*      */}

    public static int binarySearch(int[] a, int key)
    /*      */{
        /* 3697 */return binarySearch(a, key, 0, a.length);
        /*      */}

    public static int binarySearch(int[] a, int key, int start, int end)
    /*      */{
        /* 3711 */int x1 = start;
        /* 3712 */int x2 = end;
        /* 3713 */int i = x2 / 2;
        /* 3714 */while (x1 < x2) {
            /* 3715 */if (a[i] == key) {
                /* 3716 */return i;
                /*      */}
            /* 3718 */if (a[i] < key) {
                /* 3719 */x1 = i + 1;
                /*      */}
            /*      */else {
                /* 3722 */x2 = i;
                /*      */}
            /* 3724 */i = x1 + (x2 - x1) / 2;
            /*      */}
        /* 3726 */return -1 * i;
        /*      */}
}

Related

  1. binarySearch(int[] source, int key)
  2. binarySearch(long[] a, int fromIndex, int toIndex, long key)
  3. binarySearch(long[] a, long key, int endIndex)
  4. binarySearch(long[] array, long key)
  5. binarySearch(long[] data, long key, int low, int high)
  6. binarySearch(String[] arr, String value)
  7. binarySearch(String[] array, String sought)
  8. binarySearch(T[] a, T x)
  9. binarySearch0(float[] a, int fromIndex, int toIndex, float key)