二分查找java模板

    public static int binarySearch(int[] array,int first,int last,int value){
        while(first<last){
            int mid = first+(last-first)/2;
            if(array[mid]<value){
                first = mid+1;
            }
            else{
                last = mid;
            }
        }
        if(array[first]!=value){
            return -1;
        }
        return first;
    }

最好用的二分查找模板。

查找[first,last)之间的value值,如果不存在返回-1。有重复数会返回第一个。

发布了48 篇原创文章 · 获赞 0 · 访问量 4320

猜你喜欢

转载自blog.csdn.net/weixin_41327340/article/details/103878166
今日推荐