二分查找

public class BinarySelect {

    public static void main(String[] args) {
        int []array = {7,22,33,48,89,89,111,199,223,588};

        System.out.println("下标为" + binarySelect(array, 22));

//        for (int i: array){
//            System.out.println(i);
//        }



    }

    private static int binarySelect(int[] array, int i) {

        int start = 0;
        int end = array.length-1;
        int mid = 0;
        while (start < end){
            mid= (start + end)/2;
            if (array[mid] > i){
                end = mid - 1;
            }else if(array[mid] < i)
                start = mid + 1;
            else
                return mid;
        }
        return -1;


    }


}

 

posted @ 2020-10-27 19:29  执着的乌龟  阅读(71)  评论(0编辑  收藏  举报