数据结构与算法——二分查找

数据结构与算法——二分查找


二分查找对有序数组进行查找,循环将待查找的值与数组中点值比较,并缩小值所在范围,直到找到值

1、常规二分查找

public class BinarySearch {

    public static void main(String[] args) {
        final SecureRandom random = new SecureRandom();
        final int[] arr = new int[100];
        for (int i=0;i<100;i++){
            arr[i] = random.nextInt(20000);
        }
        int searchValue = arr[random.nextInt(99)];
        FastSort.sort(arr);
        System.out.println("排序后的数组:"+Arrays.toString(arr));
        System.out.println("要找的值:"+searchValue);
        int searchIndex = search(arr, searchValue);
        System.out.println("找到的位置:"+ searchIndex);
        System.out.println("位置上的值:" + arr[searchIndex]);
    }

    public static int search(int[] arr,int value){
        int minIndex=0;
        int maxIndex=arr.length-1;
        while (minIndex <= maxIndex){
            //加差的一半,是防止大数相加溢出;位运算是为了快
            int midIndex = minIndex + ((maxIndex - minIndex) >> 1);
            if(value < arr[midIndex]){
                //要找的值比中间值小,就把范围缩小到前一半
                maxIndex = midIndex-1;
            }else if(value > arr[midIndex]){
                //要找的值比中间值大,就把范围缩小到后一半
                minIndex = midIndex+1;
            }else {
                return midIndex;
            }
        }
        return -1;
    }
}

2、查找第一个值等于给定值的元素

public class BinarySearch {
    public static void main(String[] args) {
        final SecureRandom random = new SecureRandom();
        final int[] arr = new int[500];
        for (int i = 0; i < 500; i++) {
            arr[i] = random.nextInt(100);
        }
        int searchValue = arr[random.nextInt(499)];
        FastSort.sort(arr);
        System.out.println("排序后的数组:" + Arrays.toString(arr));
        System.out.println("要找的值:" + searchValue);
        int searchIndex = searchFirstEq(arr, searchValue);
        System.out.println("找到的位置:" + searchIndex);
        System.out.println("位置上的值:" + arr[searchIndex]);
        if (searchIndex != 0) {
            System.out.println("前一个位置上的值:" + arr[searchIndex - 1]);
        }
    }
    public static int searchFirstEq(int[] arr, int value) {
        int minIndex = 0;
        int maxIndex = arr.length - 1;
        while (minIndex <= maxIndex) {
            //加差的一半,是防止大数相加溢出;位运算是为了快
            int midIndex = minIndex + ((maxIndex - minIndex) >> 1);
            if (value < arr[midIndex]) {
                //要找的值比中间值小,就把范围缩小到前一半
                maxIndex = midIndex - 1;
            } else if (value > arr[midIndex]) {
                //要找的值比中间值大,就把范围缩小到后一半
                minIndex = midIndex + 1;
            } else {
              	//已经找到要找的值了
                if (midIndex == 0 || arr[midIndex - 1] < value) {
                  	//是数组第一个元素,他前一个元素比他小,说明这是第一个值等于给定值的元素
                    return midIndex;
                } else {
                  	//说明前面还有相同元素,继续向前二分查找
                    maxIndex = midIndex - 1;
                }
            }
        }
        return -1;
    }
}

3、查找最后一个值等于给定值的元素

public class BinarySearch {
    public static void main(String[] args) {
        final SecureRandom random = new SecureRandom();
        final int[] arr = new int[500];
        for (int i = 0; i < 500; i++) {
            arr[i] = random.nextInt(100);
        }
        int searchValue = arr[random.nextInt(499)];
        FastSort.sort(arr);
        System.out.println("排序后的数组:" + Arrays.toString(arr));
        System.out.println("要找的值:" + searchValue);
        int searchIndex = searchLastEq(arr, searchValue);
        System.out.println("找到的位置:" + searchIndex);
        System.out.println("位置上的值:" + arr[searchIndex]);
        if(searchIndex != arr.length-1){
            System.out.println("后一个位置上的值:" + arr[searchIndex + 1]);
        }
    }
    public static int searchLastEq(int[] arr, int value) {
        int minIndex = 0;
        int maxIndex = arr.length - 1;
        while (minIndex <= maxIndex) {
            //加差的一半,是防止大数相加溢出;位运算是为了快
            int midIndex = minIndex + ((maxIndex - minIndex) >> 1);
            if (value < arr[midIndex]) {
                //要找的值比中间值小,就把范围缩小到前一半
                maxIndex = midIndex - 1;
            } else if (value > arr[midIndex]) {
                //要找的值比中间值大,就把范围缩小到后一半
                minIndex = midIndex + 1;
            } else {
                if (midIndex == arr.length - 1 || arr[midIndex + 1] > value) {
                    return midIndex;
                } else {
                    minIndex = midIndex - 1;
                }
            }
        }
        return -1;
    }
}

4、查找第一个大于等于给定值的元素

public class BinarySearch {
    public static void main(String[] args) {
        final SecureRandom random = new SecureRandom();
        final int[] arr = new int[500];
        for (int i = 0; i < 500; i++) {
            arr[i] = random.nextInt(100);
        }
        int searchValue = arr[random.nextInt(500)];
        FastSort.sort(arr);
        System.out.println("排序后的数组:" + Arrays.toString(arr));
        System.out.println("要找的值:" + searchValue);
        int searchIndex = searchFirstMoreOrEq(arr, searchValue);
        System.out.println("找到的位置:" + searchIndex);
        System.out.println("位置上的值:" + arr[searchIndex]);
        if (searchIndex != arr.length - 1) {
            System.out.println("前一个位置上的值:" + arr[searchIndex - 1]);
        }
    }
    public static int searchFirstMoreOrEq(int[] arr, int value) {
        int minIndex = 0;
        int maxIndex = arr.length - 1;
        while (minIndex <= maxIndex) {
            //加差的一半,是防止大数相加溢出;位运算是为了快
            int midIndex = minIndex + ((maxIndex - minIndex) >> 1);
            if (arr[midIndex] >= value) {
                if (midIndex == 0 || arr[midIndex - 1] < value) {
                    return midIndex;
                } else {
                    //中间值比给定值大或相等,就把范围缩小到前一半
                    maxIndex = midIndex - 1;
                }
            } else {
                //中间值比给定值小,说明期望值在中间值右侧,将范围缩小到后面一半
                minIndex = midIndex + 1;
            }
        }
        return -1;
    }
}

5、查找最后一个小于等于给定值的元素

package com.lcy.data_structure_and_algorithm;

import java.security.SecureRandom;
import java.util.Arrays;

/**
 * 二分查找/折半查找
 */
public class BinarySearch {
    public static void main(String[] args) {
        final SecureRandom random = new SecureRandom();
        int[] arr = new int[500];
        for (int i = 0; i < 500; i++) {
            arr[i] = random.nextInt(100);
        }
        int searchValue = arr[random.nextInt(500)];
        FastSort.sort(arr);
        System.out.println("排序后的数组:" + Arrays.toString(arr));
        System.out.println("要找的值:" + searchValue);
        int searchIndex = searchLastLessOrEq(arr, searchValue);
        System.out.println("找到的位置:" + searchIndex);
        System.out.println("位置上的值:" + arr[searchIndex]);
        if (searchIndex != arr.length - 1) {
            System.out.println("后一个位置上的值:" + arr[searchIndex + 1]);
        }
    }
    public static int searchLastLessOrEq(int[] arr, int value) {
        int minIndex = 0;
        int maxIndex = arr.length - 1;
        while (minIndex <= maxIndex) {
            //加差的一半,是防止大数相加溢出;位运算是为了快
            int midIndex = minIndex + ((maxIndex - minIndex) >> 1);
            if (value >= arr[midIndex]) {
                if (midIndex == arr.length-1 || arr[midIndex + 1] > value) {
                    return midIndex;
                } else {
                    //中间值比给定值小或相等,就把范围缩小到后一半
                    minIndex = midIndex + 1;
                }
            } else {
                maxIndex = midIndex - 1;
            }
        }
        return -1;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wheat_Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值