二分查找及其变种

二分查找

本文参考https://www.cnblogs.com/luoxn28/p/5767571.html

1. 浙大数据结构老师用了查坏电线杆这个例子来解释了二分查找

比如上海到杭州的有线电话打不通了,用传统的查找办法就是从上海出发起的第一条电线杆开始找,一直找下去,这样的时间复杂度虽然是O(n),但是因为n非常大,所以是一个相当笨拙的办法。

如果用二分查找的思想就是先到上海和杭州的中间,然后分别向上海和杭州打电话,如果哪一边不通了,就说明是到上海还是杭州的路线出现故障了,然后再到故障的那一半路的中间向两端打电话,再二分下去…这样子,它的复杂度就是O (log(n))而已

2. 普通二分查找的代码实现

其实普通的二分查找方法挺好理解的,但是真正写起代码来可能会出现些细节的问题,反正这个代码也不长,在理解的基础上把它给背了,最好能默下来,LeetCode那里有很多二分的练习题,最好找时间多练练。

public class Main {
    public static void main(String[] args) {
        //查找的数据范围
        int[] arr = {1, 3, 5, 7, 8, 10};
        //要查找的那个数
        int key = 7;
        //利用二分查找找出key在arr数组中的位置,如果不存在这个数就返回-1
        int position = bs(arr, key);
        //输出key在数组中的位置
        System.out.println(position);  //3
    }

    /**
     * 二分查找
     * @param arr 被查找的数组(数据范围)
     * @param key 需要查找的数
     * @return 找到就返回它的位置,否则返回-1
     */
    private static int bs(int[] arr, int key) {
        int begin = 0;
        int end = arr.length - 1;
        while (begin <= end) {
            int mid = (begin + end) / 2;
            if (arr[mid] > key) {
                end = mid - 1;
            }
            else if(arr[mid] < key){
                begin = mid + 1;
            }else if(arr[mid] == key){
                return mid;
            }
        }
        return -1;
    }
}

3. 二分查找的变种1:找到小于等于key的最后一个数

假如我要找的数据key = 11在arr数组中并不存在

//查找的数据范围
int[] arr = {1, 3, 5, 7, 8, 10};
//要查找的那个数
int key = 11;

那么我要求如果这个数据key在这里面没有,就返回小于或等于key的最后一个数,那么这里应该返回10,其它数据虽然都小于等于11,但只有10是小于等于11的最后一个数

代码实现
public class Main {
    public static void main(String[] args) {
        //查找的数据范围
        int[] arr = {1, 3, 5, 7, 8, 10};
        //要查找的那个数
        int key = 11;
        //利用二分查找找出小于等于key的最后一个数
        int num = bs(arr, key);
        System.out.println(num); //10
    }

    /**
     * 二分查找
     * @param arr 被查找的数组(数据范围)
     * @param key 需要查找的数
     * @return 返回小于等于key的最后一个数
     */
    private static int bs(int[] arr, int key) {
        int begin = 0;
        int end = arr.length - 1;
        
        while (begin <= end) {
            int mid = (begin + end) / 2;
            if (arr[mid] > key) {
                end = mid - 1;
            }
            else {
                begin = mid + 1;
            }
        }
        return arr[end];
    }
}

4. 二分查找变种2:找到大于等于key的第一个数

假如我要找的数据key = 6在arr数组中并不存在

//查找的数据范围
int[] arr = {1, 3, 5, 7, 8, 10};
//要查找的那个数
int key = 6;

那么我要求如果这个数据key这里面没有,就返回大于或等于key的第一个数,那么这里应该返回7,其它数据8、10都大于等于6,但只有7是大于等于6的第一个数

代码实现:和变种一里面的代码差不多,只改了两个小地方
public class Main {
    public static void main(String[] args) {
        //查找的数据范围
        int[] arr = {1, 3, 5, 7, 8, 10};
        //要查找的那个数
        int key = 6;
        //利用二分查找找出大于等于key的第一个数
        int num = bs(arr, key);
        System.out.println(num); //7
    }

    /**
     * 二分查找
     * @param arr 被查找的数组(数据范围)
     * @param key 需要查找的数
     * @return 返回大于等于key的第一个数
     */
    private static int bs(int[] arr, int key) {
        int begin = 0;
        int end = arr.length - 1;

        while (begin <= end) {
            int mid = (begin + end) / 2;
            if (arr[mid] >= key) {
                end = mid - 1;
            }
            else {
                begin = mid + 1;
            }
        }
        return arr[begin];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值