I'm writing a binary search algorithm using recursion and I just don't know how to start. Here's what I have so far:
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)
{
}
}
At the bottom under the "public int binarcySearch()" section is where I am stuck. I think I'll need a few if statements with returns and maybe some other things, but I don't know exactly what. I know what I'm supposed to do, but not how to do it. Here are some hints from the book which I'm not sure how to implement:
- If your lowIndex input is greater than your highIndex, then return -1 because you have finished searching the array and cannot find the target value.
- Calculate an integer midIndex value using the formula described in the binary search discussion: midIndex = lowIndex + (highIndex - lowIndex) / 2.
- Check the value of the target array at the midIndex. If it matches your targetValue, you are done, so return your midIndex as the final result!
- If your targetValue was not found, then you need to recursively call binarySearch(), modifying the lowIndex and highIndex parameters to remove parts of the array that don’t contain the target.
- If the middle value too high, use the existing lowIndex and a highIndex equal to midIndex -1 in your recursive function call.
- If the middle value too low, use a lowIndex equal to midIndex + 1 and the existing highIndex in your recursive function call
- Your recursive binarySearch() call will return the index of the target value or -1 if not found, so you can return that result directly from the parent binarySearch() code.
Please keep in mind that I am a very early, beginner, baby programmer and the DIY class I'm taking sucks at explaining things. So please be simple and clear. Thank you.