Java 如何使用二进制搜索搜索一系列值?

Java 如何使用二进制搜索搜索一系列值?,java,range,binary-search,Java,Range,Binary Search,我正在尝试编写一个程序,它将接受一个排序整数的ArrayList,并且会有一个二进制搜索方法,在这个方法中指定要从ArrayList返回的范围和值 import java.util.ArrayList; 公共类数组搜索{ ArrayList myArrayList=新建ArrayList(); 静态ArrayList范围=新建ArrayList(); 公共静态ArrayList二进制搜索(ArrayList ArrayList、int-min、int-max、int-first、int-last

我正在尝试编写一个程序,它将接受一个排序整数的ArrayList,并且会有一个二进制搜索方法,在这个方法中指定要从ArrayList返回的范围和值

import java.util.ArrayList;
公共类数组搜索{
ArrayList myArrayList=新建ArrayList();
静态ArrayList范围=新建ArrayList();
公共静态ArrayList二进制搜索(ArrayList ArrayList、int-min、int-max、int-first、int-last)
抛出NotFoundException{
如果(第一个>最后一个){
抛出newnotfoundexception(“未找到元素”);
}
否则{
中间整数=(第一个+最后一个)/2;
int mid_number=arrayList.get(中间);

如果(mid_number>=min&&mid_number首先查找元素的较低索引范围,然后查找元素的较高索引范围。 要查找较低的索引范围,当您找到元素时,不要像我们在正常搜索中那样跳出循环。将值放入索引中,使中间值小于您正在检查的当前索引。这将使循环再次查找较低的索引,而不是在第一个索引处停止。 同样,对于上面的索引,将start更新为我们正在检查的当前索引+1。 看看代码

public ArrayList<Integer> searchRange(ArrayList<Integer> a, int b) {
    int first=lowerIndex(a,b);
    int last=upperIndex(a,b);
    ArrayList<Integer> result= new ArrayList<Integer>();
    result.add(first);
    result.add(last);
    return result;

}

public int upperIndex(ArrayList<Integer> a, int b) {
    int index=-1; //will return -1 if element not found.
    int start=0;
    int end=a.size()-1;

    while(start<=end){
        int mid=(start+end)/2;
        if(a.get(mid)==b){
            index=mid;    //Update Index, which is the upper index here.
            start=mid+1; // This is the main step.
        }
        else if(a.get(mid)>b){
            end=mid-1;
        }
        else{
            start=mid+1;
        }
    }


    return index;
}

public int lowerIndex(ArrayList<Integer> a, int b) {
    int index=-1; // will return -1 if element not found
    int start=0;
    int end=a.size()-1;

    while(start<=end){
        int mid=(start+end)/2;
        if(a.get(mid)==b){
            index=mid;  //Update Index, which is the lower index here.
            end=mid-1; // This is the main step .
        }
        else if(a.get(mid)>b){
            end=mid-1;
        }
        else{
            start=mid+1;
        }
    }

    return index;
}
公共ArrayList搜索范围(ArrayList a,int b){
int first=下限指数(a,b);
int last=upperIndex(a,b);
ArrayList结果=新建ArrayList();
结果。添加(第一);
结果。添加(最后一个);
返回结果;
}
公共整数索引(数组列表a,整数b){
int index=-1;//如果找不到元素,则返回-1。
int start=0;
int end=a.size()-1;
while(startb){
end=mid-1;
}
否则{
开始=中间+1;
}
}
收益指数;
}
公共内部lowerIndex(数组列表a,内部b){
int index=-1;//如果找不到元素,则返回-1
int start=0;
int end=a.size()-1;
while(startb){
end=mid-1;
}
否则{
开始=中间+1;
}
}
收益指数;
}

您可以对范围的最小值进行二进制搜索,然后再搜索最大值,并返回介于两者之间的所有内容。您也可以先找到最小值,然后从那里线性迭代,直到超过最大值。无论如何,建立返回值都需要线性时间。Bula您能否详细说明如何执行此操作,因为我不是su重新
public ArrayList<Integer> searchRange(ArrayList<Integer> a, int b) {
    int first=lowerIndex(a,b);
    int last=upperIndex(a,b);
    ArrayList<Integer> result= new ArrayList<Integer>();
    result.add(first);
    result.add(last);
    return result;

}

public int upperIndex(ArrayList<Integer> a, int b) {
    int index=-1; //will return -1 if element not found.
    int start=0;
    int end=a.size()-1;

    while(start<=end){
        int mid=(start+end)/2;
        if(a.get(mid)==b){
            index=mid;    //Update Index, which is the upper index here.
            start=mid+1; // This is the main step.
        }
        else if(a.get(mid)>b){
            end=mid-1;
        }
        else{
            start=mid+1;
        }
    }


    return index;
}

public int lowerIndex(ArrayList<Integer> a, int b) {
    int index=-1; // will return -1 if element not found
    int start=0;
    int end=a.size()-1;

    while(start<=end){
        int mid=(start+end)/2;
        if(a.get(mid)==b){
            index=mid;  //Update Index, which is the lower index here.
            end=mid-1; // This is the main step .
        }
        else if(a.get(mid)>b){
            end=mid-1;
        }
        else{
            start=mid+1;
        }
    }

    return index;
}