Class BinarySearcher

java.lang.Object
  extended by BinarySearcher

public class BinarySearcher
extends java.lang.Object

The BinarySearcher Java application runs a small test on the search method from Chapter 11 (using a binary search to find a specified number in an array).

Java Source Code for this class:
http://www.cs.colorado.edu/~main/applications/SimpleSearcher.java


Constructor Summary
BinarySearcher()
           
 
Method Summary
static void main(java.lang.String[] args)
          The main method prints a table of test results, searching for numbers in an array that contains 2, 4, 6, 8, 10, 12, and 14.
static int search(int[] a, int first, int size, int target)
          Search part of a sorted array for a specified target.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BinarySearcher

public BinarySearcher()
Method Detail

main

public static void main(java.lang.String[] args)
The main method prints a table of test results, searching for numbers in an array that contains 2, 4, 6, 8, 10, 12, and 14. The String arguments (args) are not used in this implementation.


search

public static int search(int[] a,
                         int first,
                         int size,
                         int target)
Search part of a sorted array for a specified target.

Parameters:
a - the array to search
first - the first index of the part of the array to search
size - the number of elements to search, starting at a[first]
target - the element to search for
Precondition:
If size > 0, then first through first+size-1 must be valid indexes for the array a. Also, starting at a[first], the next size elements are sorted in increasing order from small to large.
Returns:
If target appears in the array segment starting at a[first] and containing size elements, then the return value is the index of a location that contains target; otherwise the return value is -1.
Throws:
ArrayIndexOutOfBounds - Exception Indicates that some index in the range first through first+size-1 is outside the range of valid indexes for the array a.