Java递归二进制搜索
我正在写一个使用递归的二进制搜索算法,我只是不知道如何开始。以下是我目前掌握的情况:Java递归二进制搜索,java,recursion,binary-search,Java,Recursion,Binary Search,我正在写一个使用递归的二进制搜索算法,我只是不知道如何开始。以下是我目前掌握的情况: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class BinarySearch implements ActionListener { public static void main(String[] args) { new BinarySearch(); }
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BinarySearch implements ActionListener
{
public static void main(String[] args)
{
new BinarySearch();
}
private JSpinner searchSpinner;
private JButton searchButton;
private JList searchList;
Integer[] myNumbers = {1, 3, 5, 6, 8, 9, 10, 12, 14, 15};
public BinarySearch()
{
JFrame myFrame = new JFrame(); // create the JFrame window
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = (JPanel)myFrame.getContentPane();
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
mainPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
searchSpinner = new JSpinner(new SpinnerNumberModel(5,0,99,1));
searchButton = new JButton("Search");
searchButton.addActionListener(this);
searchButton.setAlignmentX(Component.CENTER_ALIGNMENT);
searchList = new JList(myNumbers);
searchList.setFixedCellWidth(50);
searchList.setVisibleRowCount(myNumbers.length);
JLabel label = new JLabel("Target Value");
label.setAlignmentX(Component.CENTER_ALIGNMENT);
mainPanel.add(label);
mainPanel.add(searchSpinner);
mainPanel.add(Box.createRigidArea(new Dimension(0,5)));
mainPanel.add(searchButton);
mainPanel.add(Box.createRigidArea(new Dimension(0,5)));
mainPanel.add(searchList);
myFrame.pack();
myFrame.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
Object control = event.getSource();
if (control == searchButton)
{
searchList.clearSelection();
int targetValue = (Integer)searchSpinner.getValue();
int index = binarySearch(myNumbers,targetValue,0,myNumbers.length-1);
if (index >= 0)
{
searchList.setSelectedIndex(index);
}
else
{
JOptionPane.showMessageDialog(null, "Number " + targetValue + " not found!");
}
}
}
public int binarySearch(Integer[] targetArray, int targetValue, int lowIndex, int highIndex)
{
}
}
在“public int binarcySearch()”部分的底部,我被卡住了。我想我需要一些带返回的if语句,也许还有其他一些东西,但我不知道具体是什么。我知道我应该做什么,但不知道怎么做。以下是本书中的一些提示,我不确定如何实施:
- 如果lowIndex输入大于highIndex,则返回-1,因为您已完成对数组的搜索,无法找到目标值
- 使用二进制搜索讨论中描述的公式计算整数midIndex值: midIndex=低索引+(高索引-低索引)/2
- 在midIndex处检查目标数组的值。如果它与targetValue匹配,那么就完成了,因此返回midIndex作为最终结果李>
- 如果找不到targetValue,则需要递归调用binarySearch(),修改lowIndex和highIndex参数以删除数组中不包含目标的部分。
- 如果中间值太高,请在递归函数调用中使用现有的lowIndex和等于midIndex-1的highIndex
- 如果中间值太低,请在递归函数调用中使用等于midIndex+1的lowIndex和现有的highIndex
- 递归binarySearch()调用将返回目标值的索引,如果未找到,则返回-1,因此可以直接从父binarySearch()代码返回该结果
public int binarySearch(Integer[] targetArray, int targetValue, int lowIndex, int highIndex)
{
if (lowIndex > highIndex) return -1;
int midIndex = lowIndex + (highIndex - lowIndex) / 2;
if (targetArray[midIndex] == targetValue) return midIndex;
if (targetArray[midIndex] > targetValue)
return binarySearch(targetArray, targetValue, lowIndex, midIndex-1);
else
return binarySearch(targetArray, targetValue, midIndex+1, highIndex);
}
这是您的代码:
public int binarySearch(Integer[] targetArray, int targetValue, int lowIndex, int highIndex)
{
if (lowIndex > highIndex) return -1;
int midIndex = lowIndex + (highIndex - lowIndex) / 2;
if (targetArray[midIndex] == targetValue) return midIndex;
if (targetArray[midIndex] > targetValue)
return binarySearch(targetArray, targetValue, lowIndex, midIndex-1);
else
return binarySearch(targetArray, targetValue, midIndex+1, highIndex);
}
注意:回答是因为我没有代表评论
这太令人沮丧了。好的,您复制的书中的提示是关于如何实现binarySearch()方法的规范。我建议学习如何解决这个问题是一步一步地接受每个提示语句,使用到目前为止您已经学习的各种流控制语句,并查看结果是否通过测试
事实上,这就是今天有多少专业开发人员在工作。我们编写的测试用例描述了我们在实际编写代码之前想要完成的事情的结果,因为我们知道它会失败。当它通过我们的测试时,我们就完成了
由于说明不清楚,谷歌搜索“java二进制搜索”为您带来了什么?像二进制搜索这样的计算机科学基础知识有许多可能对学生更好的例子和解释
可能会有帮助。注意:回答是因为我没有代表发表评论
这太令人沮丧了。好的,您复制的书中的提示是关于如何实现binarySearch()方法的规范。我建议学习如何解决这个问题是一步一步地接受每个提示语句,使用到目前为止您已经学习的各种流控制语句,并查看结果是否通过测试
事实上,这就是今天有多少专业开发人员在工作。我们编写的测试用例描述了我们在实际编写代码之前想要完成的事情的结果,因为我们知道它会失败。当它通过我们的测试时,我们就完成了
由于说明不清楚,谷歌搜索“java二进制搜索”为您带来了什么?像二进制搜索这样的计算机科学基础知识有许多可能对学生更好的例子和解释
可能会有帮助。我的电脑上有几个程序,除了我父母允许的以外,基本上不允许我访问任何网站。基本上我的电子邮件地址是一些军事网站和这一个。所以我无法用谷歌或其他方式搜索任何东西。否则我肯定会有哦,链接非常有用。非常感谢!我的电脑上有几个程序,除了我父母允许的以外,基本上不允许我访问任何网站。基本上我的电子邮件地址是一些军事网站和这一个。所以我无法用谷歌或其他方式搜索任何东西。否则我肯定会有哦,链接非常有用。非常感谢!谢谢你的密码。为了可读性,我对它做了一点修改,但基本上只是复制/粘贴。我没有把你的答案选为“赢家”的唯一原因是因为我看到了笑声,但我们会先看到负责人的答案,而且这也帮助我理解为什么我应该使用你的答案。谢谢,不客气。尽管如此,您还是应该再次尝试自己编写代码。这实际上是一个翻译,英语->Java:-)@agim只是给他答案有点违背了他学习它的目的,不是吗?是和否。如果他不知道如何开始,那他最好有一个例子,通过阅读代码来学习。谢谢你的代码。为了可读性,我对它做了一点修改,但基本上只是复制/粘贴。我没有把你的答案选为“赢家”的唯一原因是因为我看到了笑声,但我们会先看到负责人的答案,而且这也帮助我理解为什么我应该使用你的答案。谢谢,不客气。尽管如此,您还是应该再次尝试自己编写代码。这实际上是一个翻译,英语->Java:-)@agim只是给他一个答案,这有点违背了他学习它的目的,不是吗?是的,也不是。如果他不知道如何开始,那他最好举个例子,通过阅读代码来学习。