Click to See Complete Forum and Search --> : Binary and Linear Search


ForlornSpawn
04-07-2006, 10:23 AM
I am trying to implement a Binary search both recursively and iteratively and a Linear search in separate classes for an assignment. I can't seem to figure out why my binary searches are coming out with the same number of searches as my linear search. Also, I would like to know if there is a way to generate a random number array which does not have repeating numbers.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

class Search extends JPanel
{
JButton search;
JLabel prompt,binIt,binRec,lin,found;
JTextField input;

final String linText = "Number of Linear searches: ",
binItText = "Number of iterative Binary searches: ",
binRecText = "Number of recursive Binary searches: ";

int[] numArray = new int[99];
int key;

public int[] createArray()
{
for(int i =0;i<numArray.length;i++)
numArray[i] = i;
return numArray;
}

public Search()
{
JPanel jp = new JPanel();

jp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;

prompt = new JLabel("Enter the value to search for: ");
c.gridx = 0;
c.gridy = 0;
jp.add(prompt,c);

input = new JTextField(5);
c.gridx = 1;
c.gridy = 0;
jp.add(input,c);

search = new JButton("Search");
c.gridx = 3;
c.gridy = 0;
jp.add(search,c);

found = new JLabel();
c.gridx = 0;
c.gridy = 1;
jp.add(found,c);

binIt = new JLabel();
c.gridx = 0;
c.gridy = 2;
jp.add(binIt,c);

binRec = new JLabel();
c.gridx = 0;
c.gridy = 3;
jp.add(binRec,c);

lin = new JLabel();
c.gridx = 0;
c.gridy = 4;
jp.add(lin,c);

search.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
key = Integer.parseInt(input.getText());

BinIt bI = new BinIt(createArray(),key);
binIt.setText(binItText + bI.bISearch());

binRec.setText(binRecText +
BinRec.bRSearch(createArray(),0,createArray().length-1,key));

Linear myLin = new Linear(createArray(),key);
lin.setText(linText + myLin.lSearch());

if((bI.bISearch() != -1 || BinRec.bRSearch(createArray(),createArray()[0],createArray().length,key) != -1
|| myLin.lSearch() != -1))
found.setText("Number was found!");
else
found.setText("Number was not found.");
}
} );

add(jp);
}

public static void main(String args[])
{
JFrame f = new JFrame();
f.setTitle("Search");
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,300);
Container contentPane = f.getContentPane();
contentPane.add(new Search());
f.setVisible(true);
}
}

class BinIt
{
int[] bIArray;
int bIKey;

public BinIt(int a[],int k)
{
bIArray = a;
bIKey = k;
}
public int bISearch()
{
int low = bIArray[0];
int high = bIArray.length - 1;
int mid;

if(bIKey > bIArray[bIArray.length-1] || bIKey < bIArray[0])
return -1;
while (low <= high)
{
mid = (low + high)/2;
if (bIKey > bIArray[mid])
low = mid + 1;
else if (bIKey < bIArray[mid])
high = mid - 1;
else
return mid;
}
return -1;
}
}

class BinRec
{
public static int bRSearch(int[] array, int low, int high, int value)
{
if(value > array[array.length-1] || value < array[0])
return -1;
if (low <= high)
{
int mid = (low + high)/2;
if (value > array[mid])
return bRSearch(array, mid + 1, high, value);
else if (value < array[mid])
return bRSearch(array, low, mid - 1, value);
else
return mid;
}
else
return -1;
}
}
class Linear
{
int[] linArray;
int lKey;
public Linear(int a[], int k)
{
linArray = a;
lKey = k;
}

public int lSearch()
{
int counter;

for(counter = 0; counter<linArray.length; counter++ )
{
if(lKey == linArray[counter])
return counter;
}
return -1;
}
}

nspils
04-07-2006, 02:43 PM
I would put the counter inside of each search method, and this would be the value returned - otherwise, how are you counting the number of guesses the various search methods are using? There is no passing of this information out of the search methods.

Some general comments:

Your methods seem to be designed specifically for your "test case" and not for generalized use.

In your binary search method, you are assigning high to be the value of the last element of the array, you are assigning low to be the value at the first element of the array and using mid as one-half of their sum. That works ok if you are using values the same as the elements (your test case), but it won't work when you are randomly assigning values. Therefore, you need to be using mid as the floor of 1/2 of the size [so you are finding the range of the elements, not the range of the values].

You'll find that binary search only works if your set is sorted. You don't have that requirement if you are using a linear search. So you'll have to add some mechanism to sort the array before conducting a binary search ... and if you are comparing execution time, you'll need to do that inside of your binary search methods to be fair to the linear search method.