SUMMARY | 二分查找

Posted ustctp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SUMMARY | 二分查找相关的知识,希望对你有一定的参考价值。

package Search;

public class biSearch {
    //标准的二分查找
    public static int stdBiSearch(int[] array,int keyValue) {
        int length=array.length;
        int left=0,right=length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(keyValue<array[mid]) right=mid-1;
            else if(keyValue>array[mid]) left=mid+1;
            else return mid;
        }
        return -1;
    }
    
    //(二分法)查找第一个小于keyValue的元素
    public static int BiSearchToFindFirstSmaller(int[] array,int keyValue) {
        int length=array.length;
        int left=0,right=length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(keyValue<=array[mid]) right=mid-1;
            else left=mid+1;
        }
        return right;
    }
    
    //(二分法)查找第一个大于等于keyValue的元素
    public static int BiSearchToFindFirstBiggerorEqual(int[] array,int keyValue) {
        int length=array.length;
        int left=0,right=length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(keyValue<=array[mid]) right=mid-1;
            else left=mid+1;
        }
        return left;
    }
    
    //(二分法)查找第一个最后一个小于等于keyValue的元素
    public static int BiSearchToFindLastSmallerorEqual(int[] array,int keyValue) {
        int length=array.length;
        int left=0,right=length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(keyValue<array[mid]) right=mid-1;
            else left=mid+1;
        }
        return right;
    }
    
    //(二分法)查找第一个第一个大于keyValue的元素
    public static int BiSearchToFindFirstBigger(int[] array,int keyValue) {
        int length=array.length;
        int left=0,right=length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(keyValue<array[mid]) right=mid-1;
            else left=mid+1;
        }
        return left;
    }
    
    //(二分法)查找第一个第一个等于keyValue的元素
    public static int BiSearchToFindFirst(int[] array,int keyValue) {
        int length=array.length;
        int left=0,right=length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(keyValue<=array[mid]) right=mid-1;
            else left=mid+1;
        }
        //array[right]<keyValue<=array[left]
        if(left<length&&array[left]==keyValue) return left;
        else return -1;
    }
    
    //(二分法)查找第一个最后一个等于keyValue的元素
    public static int BiSearchToFindLast(int[] array,int keyValue) {
        int length=array.length;
        int left=0,right=length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(keyValue<array[mid]) right=mid-1;
            else left=mid+1;
        }
        //array[right]<=keyValue<array[left]
        if(right>=0&&array[right]==keyValue) return right;
        else return -1;
    }
    
    //测试样例
    public static void main(String[] args) {
        int[] a=new int[]{0,1,2,2,2,5,6};
        System.out.println(BiSearchToFindLast(a, 2));
    }
}

一张图帮助理解:

技术分享图片

 参考博客地址:https://www.cnblogs.com/bofengyu/p/6761389.html

以上是关于SUMMARY | 二分查找的主要内容,如果未能解决你的问题,请参考以下文章

查找--二分法查找

二分搜索算法

折半算法,也就是二分查找法

今日头条2018校招后端方向(第二批)第一题 二分查找

二分查找常见套路与分析

二分查找常见套路与分析