Java binarySort方法未返回正确项的问题

Java binarySort方法未返回正确项的问题,java,logic,binary-search,Java,Logic,Binary Search,因此,我试图让我的binarySearch工作,但它没有返回正确的搜索项。有人有主意吗?但它总是返回接近项目的内容。我知道问题特别在于binarySearch方法中的while循环 public class Sort { static int item; static Scanner console = new Scanner(System.in); public static void main(String[] args) { int[] list

因此,我试图让我的binarySearch工作,但它没有返回正确的搜索项。有人有主意吗?但它总是返回接近项目的内容。我知道问题特别在于binarySearch方法中的while循环

public class Sort { 
    static int item;
    static Scanner console = new Scanner(System.in);
    public static void main(String[] args) {
        int[] list = new int[500];
        for (int c = 0; c < 500; c++){
            Random r = new Random();
            list[c] = r.nextInt(1000);  
        }
        int l = list.length; 
        System.out.print("Enter a search element: ");
        System.out.println();
        item = console.nextInt();
        insertionSort(list, l);
    }
    public static int binarySearch(int[] list, int l, 
            int searchItem){
        int first = 0;
        int last = l - 1;
        int mid = 0;
        boolean found = false;

        while (first <= last && !found)
        {
            mid = (first + last) / 2;
            if (list[mid] == searchItem)
                found = true;
            else if (list[mid] > searchItem)
                last = mid - 1;
            else
                first = mid + 1;
        }
        if (found) 
            return mid;
        else
            return 0;
    }

    public static void insertionSort(int[] list,  int l){
        int first, location;
        int temp;
        for (first = 1; first < l; first++){
            if (list[first] < list[first - 1]){
                temp = list[first];
                location = first;
                do {
                    list[location] = list[location - 1];
                    location--;
                }
                while(location > 0 && list[location - 1] > temp);
                list[location] = temp;
            }
        }
        System.out.println(binarySearch(list, l, item));   
    }
}
公共类排序{
静态int项;
静态扫描仪控制台=新扫描仪(System.in);
公共静态void main(字符串[]args){
int[]列表=新int[500];
对于(int c=0;c<500;c++){
随机r=新随机();
列表[c]=r.nextInt(1000);
}
int l=list.length;
System.out.print(“输入搜索元素:”);
System.out.println();
item=console.nextInt();
插入排序(列表,l);
}
公共静态int二进制搜索(int[]列表,int l,
int搜索项){
int first=0;
int last=l-1;
int-mid=0;
布尔值=false;
while(第一个搜索项)
last=mid-1;
其他的
第一个=中间+1;
}
如果(找到)
中途返回;
其他的
返回0;
}
公共静态void insertionSort(int[]列表,int l){
首先,位置;
内部温度;
for(first=1;first0和列表[位置-1]>临时);
列表[位置]=温度;
}
}
System.out.println(二进制搜索(list,l,item));
}
}

谢谢你的帮助

您需要返回mid+1作为从0开始的索引

所以如果你有1,2,3个元素,如果你试图找到2,它将返回1作为数组[1]指向2。因此,如果返回2,则表示您在第二个位置找到了元素。

您的问题在于:

    if (found) 
        return mid;
假设你有一个数字为{1,2,3,4,5}的数组 根据数组索引的分配方式,1将获得0的索引,2将获得1的索引,依此类推

当您尝试搜索2并找到它时,mid的值(根据程序)保存数组中值2的索引。上述数组中2的索引为1。因此,您不需要返回索引2,而只需要返回项目本身

     if (found) 
        return list[mid];
我想说,更好的设计是从二进制搜索例程返回true/false,以指示是否找到该项。原因是如果在数组中找不到项,则返回0,如果要搜索的值中也包含0,则会出现问题

如果您试图在找到项后返回该项的索引,而不是该项本身,那么您的程序会按照预期运行,而不是在未找到该项的情况下运行—它应该返回类似于-1的索引,而不是0(有效索引)


你试过调试代码了吗。这就是我发现问题在while循环中的原因。
      else
        return 0;