I'm having problem to test a recursive version of a binary search algorithm. These are my codes:
Code:
public class OrderedArrayList extends ArrayListClass
{
//default constructor
public OrderedArrayList()
{
super();
}
//constructor with a parameter
public OrderedArrayList(int size)
{
super(size);
}
//copy constructor
public OrderedArrayList(OrderedArrayList otherList)
{
super(otherList);
}
//Method to insert insertItem in the list at the proper
//place. However, first the list is searched to
//see if the item to be inserted is already in the list.
//Postcondition: insertItem is inserted and length++
// If insertItem is already in the list or the list
// is full, an appropriate message is output.
public void insert(DataElement insertItem)
{
int first = 0;
int last = length - 1;
int mid = 0;
boolean found = false;
if(length == 0) //list is empty
{
list[0] = insertItem.getCopy();
length++;
}
else
if(length == maxSize)
System.err.println("Cannot insert into a full list.");
else
{
while(first <= last && !found)
{
mid = (first + last) / 2;
if(list[mid].equals(insertItem))
found = true;
else
if(list[mid].compareTo(insertItem) > 0)
last = mid - 1;
else
first = mid + 1;
}//end while
if(found)
System.err.println("The insert item is already in the list. "
+ "Duplicates are not allowed.");
else
{
if(list[mid].compareTo(insertItem) < 0)
mid++;
insertAt(mid, insertItem);
}
}
}//end insert
public int recBinarySearch(DataElement[] list, DataElement item, int first, int last)
{
if (first > last)
return - (list[first].compareTo(item) < 0? first++ : first);//base case for unsuccessful search
else
{
int mid = (first + last) / 2;//index for next probe
if (list[mid].equals(item))
return mid;
else if(list[mid].compareTo(item) > 0)
return recBinarySearch(list, item, first, mid - 1);//base case for successful search
else
return recBinarySearch(list, item, mid + 1, last);
}
}
public int recBinarySearch(DataElement list[], DataElement item)
{
return recBinarySearch(list, item, 0, list.length - 1);
}
}//end binarySearch
Code:
import java.io.*;
import java.util.*;
public class TestRecursiveBinarySearchProg
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException
{
OrderedArrayList intList
= new OrderedArrayList();
OrderedArrayList temp =
new OrderedArrayList();
IntElement num = new IntElement();
int counter;
int result;
StringTokenizer tokenizer;
System.out.print("Enter 10 integers on the " + "same line: ");
System.out.flush();
tokenizer = new
StringTokenizer(keyboard.readLine());
for(counter = 0; counter < 10; counter++)
{
num.setNum(Integer.parseInt(tokenizer.nextToken()));
intList.insert(num);
}
temp.copyList(intList);
System.out.println();
System.out.print("The list you " + "entered is: ");
intList.print();
System.out.println();
System.out.print("Enter the search " + "item: ");
System.out.flush();
num.setNum(Integer.parseInt(keyboard.readLine()));
System.out.println();
result = recBinarySearch(temp, num());
System.out.println("Binary search (Recursive): " + result);
}
}
Comment