我有一个我需要执行二进制搜索的int数组。如果在数组中找到用户输入的数字,则应返回它所在的索引。该索引对应于另一个阵列中的索引,然后输出该索引。问题是,对于第一次搜索,提供结果的唯一输入数字是1.如果我执行多次搜索,我的程序将失败并且不会显示任何结果。我不明白为什么我的搜索不起作用,有人可以提供一些关于我做错了什么的输入吗?
public class childrensclassics extends javax.swing.JFrame {
int[] refInts;
...
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
//declare variables
int refNumBinary = Integer.parseInt(refInput.getText());
String refNum = refInput.getText();
int binary;
//binary search
binary = binarySearch(refInts, 0, refInts.length-1, refNumBinary);
//check binary search results
if (binary == -1){
errorLabel.setText("Data not found. Please Try again.");
}
else {
binaryOutput.setText("#" + refNum + ": " + titlesArray[binary]);
}
public static Integer binarySearch(int [] A, int left, int right, int V){
if (left > right) {
return -1;
}
int middle = left + (right - left) / 2;
if (V == A[middle]) {
return middle;
}
if (V < A[middle]) {
return binarySearch(A, left, middle-1, V);
} else {
return binarySearch(A, middle + 1, right, V);
}
}
修改:
refInts数组中包含的数据样本:
1, 2, 4, 6, 10, 12, 14, 20, 24, 26,
titlesArray中包含的数据样本:
汤姆索亚历险记, 哈克贝利·芬恩, 石头之剑, Stuart Little, 宝藏岛, 秘密花园, 爱丽丝梦游仙境, 海底的二万个联盟, 彼得潘, 夏洛特的网站,
这是我的添加过程,如果有帮助,数据来自混合参考号和标题的文本文件,所有这些都在各自的行上。在我的初始代码中,我从文本文件中获取数据并将其全部放在一个名为bookList的数组中。对于二进制搜索目的,我非常确定我需要将两种不同的数据类型分开。这是一种迂回的方式,但我对java相对较新,无法想到不同的方式。
原始数组中的数据格式
1
The Adventures of Tom Sawyer
2
Huckleberry Finn
3
The Sword in the Stone
...
添加流程:
取保持参考数字的偶数索引号和保存标题的奇数索引号并将它们分开。
//separate data for binary search
for (int i = 0; i < bookList.length; i++) {
if (i%2==0) {
refNums.add(bookList[i]);
} else{
titles.add(bookList[i]);
}
}
//create searchable arrays
titlesArray = new String[(bookList.length)/2];
titles.toArray(titlesArray);
refsArray = new String[(bookList.length)/2];
refNums.toArray(refsArray);
//create int array
int[] refInts = new int[refsArray.length];
//send reference number to int array
for (int i = 0; i < refsArray.length; i++) {
refInts[i] = Integer.parseInt(refsArray[i]);
}
答案 0 :(得分:0)
我看到你在源代码中遗漏了两件事
middle = (left + right)/2;
是一个非常危险的溢出操作,必须验证
答案 1 :(得分:0)
这是您要找的样品吗?
public class TestIndex {
TestIndex() {
//Sample arrays after file load
int[] refInts = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
String[] titlesArray = {
"The Adventures of Tom Sawyer",
"Huckleberry Finn",
"The Sword in the Stone",
"Stuart Little",
"Treasure Island",
"The Secret Garden",
"Alice's Adventures in Wonderland",
"Twenty Thousand Leagues Under the Sea",
"Peter Pan",
"Charlotte's Web"
};
// Sample input captured to perform search on
int refNumBinary = 3;
//binary search
int binary = binarySearch(refInts, 0, refInts.length-1, refNumBinary);
//check binary search results
if (binary == -1){
System.out.println("Data not found. Please Try again.");
}
else {
System.out.println("#" + refNumBinary + ": " + titlesArray[binary+1]);
}
}
public static Integer binarySearch(int [] A, int left, int right, int V){
int middle;
if (left > right || right < 0) {
return -1;
}
middle = (left + right)/2;
if (V == A[middle]) {
return middle;
}
if (V < A[middle]) {
return binarySearch(A, left, middle-1, V);
} else {
return binarySearch(A, middle + 1, right, V);
}
}
public static void main(String[] args) {
new TestIndex();
}
}