二分搜索


title: 二分搜索


一个最简单的二分搜索示例,原理太简单就不介绍了,上代码:

public class BinarySearch {
    public static void main(String[] args){
        //测试数据
        int[] a = new int[]{0,1,2,3,4,5,6,7};
        System.out.print(rank(5, a));
    }

    /**
     * @param key 待查找的int
     * @param a int数组,必须为有序数据
     * @return
     */
    public static int rank(int key, int[] a){
        int lo = 0;
        int hi = a.length - 1;

        while (lo <= hi){
            int mid = lo + (hi - lo)/2;
            if(key < a[mid]){
                hi = mid - 1;
            }else if (key > a[mid]){
                lo = mid + 1;
            }else {
                return mid;
            }
        }
        return -1;
    }
}

你可能感兴趣的:(二分搜索)