
public class Search
{
    Search (int howMany, int increment)
    {
        int current = 0;
        
        numbers = new int [howMany];
        for (int i = 0; i < howMany; i++)
        {
            numbers[i] = current;
            current += increment;
        }
    }
    
    public void initializeCounts()
    {
        successfulSearches = 0;
        unsuccessfulSearches = 0;
        successfulCount = 0;
        unsuccessfulCount = 0;
    }
    
    public void printResults(String typeOfSearch)
    {
        System.out.println(typeOfSearch);
        System.out.println("--------------------");
        System.out.println("Number of successful searches = " + successfulSearches);
        if (successfulSearches > 0)
        {
            System.out.println("Average number of probes in a successful search = " + 
                (float) successfulCount / (float) successfulSearches );
        }
        System.out.println("Number of unsuccessful searches = " + unsuccessfulSearches);
        if (unsuccessfulSearches > 0)
        {
            System.out.println("Average number of probes in an unsuccessful search = " +
                (float) unsuccessfulCount / (float) unsuccessfulSearches );
        }
        System.out.println("--------------------");
        System.out.println();
    }
            
    public void linearSearch (int key)
    {
        int count = 0;
        boolean found = false;
        for (int i = 0; i < numbers.length; i++)
        {
            count++;
            if (key == numbers[i])
            {
                found = true;
                break;
            }
        }
        if (found)
        {
            successful (count);
        }
        else
        {
            unsuccessful (count);
        }
    }
    
    public void linearSearchRecursive (int key)
    {
        linearRecursive(key, 0, 0);
    }
    
    private void linearRecursive (int key, int position, int count)
    {
        if (position >= numbers.length)
        {
            unsuccessful (count);
        }
        else if (key == numbers[position])
        {
            successful (++count);
        }
        else
        {
            linearRecursive (key, position + 1, count + 1);
        }
    }
    
    public void binarySearch (int key)
    {
    }
    
    public void binarySearchRecursive (int key)
    {
        binaryRecursive (key, 0, numbers.length - 1, 0);
    }
    
    private void binaryRecursive (int key, int low, int high, int count)
    {
        int middle = (low + high) / 2;
        
        if (low > high)
        {
            unsuccessful(count);
        }
        else if (numbers[middle] == key)
        {
            successful(count + 1);
        }
        else if (key < numbers[middle])
        {
            binaryRecursive(key, low, middle - 1, count + 1);
        }
        else
        {
            binaryRecursive(key, middle + 1, high, count + 1);
        }
        
    }
    
    private void successful (int count)
    {
        successfulSearches++;
        successfulCount += count;
    }
    
    private void unsuccessful (int count)
    {
        unsuccessfulSearches++;
        unsuccessfulCount += count;
    }
            
    private int [] numbers;
    private int successfulSearches;
    private int unsuccessfulSearches;
    private int successfulCount;
    private int unsuccessfulCount;
}
