为什么此二进制搜索代码在 Eclipse IDE 上给出错误的输出?
Why does this binary search code give wrong output on Eclipse IDE?
为什么这个二进制搜索代码在 Eclipse 上给出了错误的输出 IDE 但在提交到 Coursera 时却被接受了?这是显示错误输出的示例输入。
示例输入:
5 3 2 4 1 5
3 1 2 7
输出:
-1 -1 -1
显然,存在的元素“1”是输入数组。但是它的输出是 -1 而不是 3.
import java.io.*;
import java.util.*;
public class BinarySearch {
static int binarySearch(int[] a,int l,int r,int x) {
//write your code here
if(l<=r){
int mid =l + (r - l)/2;
if(x==a[mid])
return mid;
else if(x<a[mid]){
return binarySearch(a,l,mid-1,x);
}
else
return binarySearch(a,mid+1,r,x);
}
return -1;
}
public static void main(String[] args) {
FastScanner scanner = new FastScanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
int m = scanner.nextInt();
int[] b = new int[m];
for (int i = 0; i < m; i++) {
b[i] = scanner.nextInt();
}
for (int i = 0; i < m; i++) {
//replace with the call to binarySearch when implemented
System.out.print(binarySearch(a,0,n-1,b[i]) + " ");
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}
实际上,问题出在您的输入数据上,而不是代码本身。如果搜索有关二进制搜索的信息,您可以找到:"binary search is a search algorithm that finds the position of a target value within a sorted array"。您的输入未排序。
您必须在 运行 搜索之前对数组进行排序,这不是一个好主意 - 使用其他算法进行搜索比排序花费的时间更少。
如果您尝试输入已排序的数据,例如:
5 1 2 3 4 5
3 1 2 7
结果将是 0 1 -1
- 正如预期的那样。
为什么这个二进制搜索代码在 Eclipse 上给出了错误的输出 IDE 但在提交到 Coursera 时却被接受了?这是显示错误输出的示例输入。
示例输入:
5 3 2 4 1 5
3 1 2 7
输出:
-1 -1 -1
显然,存在的元素“1”是输入数组。但是它的输出是 -1 而不是 3.
import java.io.*;
import java.util.*;
public class BinarySearch {
static int binarySearch(int[] a,int l,int r,int x) {
//write your code here
if(l<=r){
int mid =l + (r - l)/2;
if(x==a[mid])
return mid;
else if(x<a[mid]){
return binarySearch(a,l,mid-1,x);
}
else
return binarySearch(a,mid+1,r,x);
}
return -1;
}
public static void main(String[] args) {
FastScanner scanner = new FastScanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
int m = scanner.nextInt();
int[] b = new int[m];
for (int i = 0; i < m; i++) {
b[i] = scanner.nextInt();
}
for (int i = 0; i < m; i++) {
//replace with the call to binarySearch when implemented
System.out.print(binarySearch(a,0,n-1,b[i]) + " ");
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}
实际上,问题出在您的输入数据上,而不是代码本身。如果搜索有关二进制搜索的信息,您可以找到:"binary search is a search algorithm that finds the position of a target value within a sorted array"。您的输入未排序。
您必须在 运行 搜索之前对数组进行排序,这不是一个好主意 - 使用其他算法进行搜索比排序花费的时间更少。
如果您尝试输入已排序的数据,例如:
5 1 2 3 4 5
3 1 2 7
结果将是 0 1 -1
- 正如预期的那样。