Page 823 - Introduction to Programming with Java: A Problem Solving Approach
P. 823
Appendix 8 Recursion 789
/*************************************************************
*
BinarySearchDriver.java
Dean & Dean
This drives the BinarySearch class.
*************************************************************/
*
*
*
public class BinarySearchDriver
{
}
// end BinarySearchDriver class
public static void main(String[] args)
{
int[] array = new int[] {-7, 3, 5, 8, 12, 16, 23, 33, 55};
System.out.println(BinarySearch.binarySearch(
array, 0, (array.length - 1), 23));
System.out.println(BinarySearch.binarySearch(
array, 0, (array.length - 1), 4));
} // end main
Output:
first=0, last=8
first=5, last=8
⎫⎪
Ap⎪⎬ ago PDF Enhancer
Reduce range
first=5, last=6 ⎪ first=6, last=6 ⎭ found returnedValue=6 ⎫ returnedValue=6 ⎬
returnedValue=6 ⎭ 6
first=0, last=8
first=0, last=4
first=0, last=2
first=2, last=2
not found
returnedValue=-1
returnedValue=-1
returnedValue=-1
-1
and drill down.
Just return the answer.
Figure A8.5 Driver for BinarySearch class in Figure A8.4