You will implement a binary search on sorted list. Notice that this
code compiles. Items in any list you use to test this code must
implement Comparable
.
/**
* This class searches its list for a specified item
* by index
*/
import java.util.List;
public class BinarySearch<T extends Comparable>
{
private List<T> items;
/**
* This constructor accepts a List<T> of items to be searched.
* @param items the list of items we are searching
* @throws IllegalArgumentException if the list passed is not sorted
*/
public BinarySearch(List<T> items)
{
//throw an IllegalArgumentException if
//the list is out of order.
this.items = items;
}
/**
* This searches our list and returns the index containing
* <code>quarry</code>
* @param quarry the item we week
* @return the index of the first instance of <code>quarry</code>,
* or -1 if <code>quarry</code> is not found.
*/
public int indexOf(T quarry)
{
//use bianry search to locate quarry by index
//return -1 if it's a wild goose chase.
return -1;
}
public static void main(String[] args)
{
//TEST YOUR CODE!!
}
}