19.4.1 Binary Search Implementation
Class BinarySearchTest
(Fig. 19.3) contains:
• static
method binarySearch
to search an int
array for a specified key
• static
method remainingElements
to display the remaining elements in the array being searched, and
• main
to test method binarySearch
.
The main
method (lines 57–90) is nearly identical to main
in Fig. 19.2. In this program, we create a 15-element array (line 62) and line 67 calls the Arrays
class’s static
method sort to sort the array data
’s elements in an array in ascending order (by default). Recall that the binary search algorithm will work only on a sorted array. The first line of output from this program shows the sorted array of int
s. When the user instructs the program to search for ...
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.