Java集合中二分查找算法实现


Java集合中二分查找算法实现

Arrays.binarySearch实现了对有序数组特定区间的二分查找,虽然我们觉得很简单,但是阅读源码的确能看到实现这些库的优秀技巧,总是在追求完美和高效。
值得学习的地方有:
(1)边界检查;
(2)求中位数的时候使用位移操作,而不是 x/2;
(3)如果查找的元素不在数组中,通过返回值昭示了应该插入的位置,而不是直接返回-1;

public static int binarySearch(int[] a, int fromIndex, int toIndex,
                                   int key) {
        rangeCheck(a.length, fromIndex, toIndex);
        return binarySearch0(a, fromIndex, toIndex, key);
    }

    // Like public version, but without range checks.
    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
                                     int key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            int midVal = a[mid];

            if (midVal < key)
                low = mid + 1;
            else if (midVal > key)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1);  // key not found.
    }
 
同样在Collections里面同样有类似的辅助函数,不过是利用迭代器取得特定位置的元素。
public static <T>
    int binarySearch(List<? extends Comparable<? super T>> list, T key) {
        if (list instanceof RandomAccess || list.size()< BINARYSEARCH_THRESHOLD)
            return Collections. indexedBinarySearch(list, key);
        else
            return Collections. iteratorBinarySearch(list, key);
    }


private static <T>
    int indexedBinarySearch(List<? extends Comparable<? super T>> list, T key ) {
        int low = 0;
        int high = list.size()-1;

        while (low <= high) {
            int mid = ( low + high) >>> 1;
            Comparable<? super T> midVal = list.get( mid);
            int cmp = midVal.compareTo( key);

            if ( cmp < 0)
                low = mid + 1;
            else if ( cmp > 0)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1);  // key not found
    }

    private static <T>
    int iteratorBinarySearch(List<? extends Comparable<? super T>> list, T key )
    {
        int low = 0;
        int high = list.size()-1;
        ListIterator<? extends Comparable<? super T>> i = list.listIterator();

        while (low <= high) {
            int mid = ( low + high) >>> 1;
            Comparable<? super T> midVal = get(i , mid );
            int cmp = midVal.compareTo( key);

            if ( cmp < 0)
                low = mid + 1;
            else if ( cmp > 0)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1);  // key not found
    }

 /**
     * Gets the ith element from the given list by repositioning the specified
     * list listIterator.
     */
    private static <T> T get(ListIterator<? extends T> i, int index) {
        T obj = null;
        int pos = i.nextIndex();
        if (pos <= index) {
            do {
                obj = i.next();//链表移动
            } while ( pos++ < index);
        } else {
            do {
                obj = i.previous();
            } while (-- pos > index);
        }
        return obj;
    }


    




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值