public class BinarySearcher
extends java.lang.Object
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 and Description |
---|
BinarySearcher() |
Modifier and Type | Method and Description |
---|---|
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.
|
public static void main(java.lang.String[] args)
args
- not used in this implementationpublic static int search(int[] a, int first, int size, int target)
a
- the array to searchfirst
- the first index of the part of the array to searchsize
- 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.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.java.lang.ArrayIndexOutOfBoundsException
- Indicates that some index in the range first
through first+size-1
is outside the range of
valid indexes for the array a
.