|
|
Laboratory
9
Array
Searching
Week
of 11 April, 2005
|
Objective
When
you use a list or an array, you often need to be able to find values in
the list.
We will examine
two methods of searching for a particular value: sequential
and binary.
Key
Concepts
- Array manipulation and
searching
Getting Started
Using the procedures in the
previous laboratories, copy the
files Sequential.java,
Searcher.java, Binary.java,
and BinarySearcher.java
for your manipulation.
Sequential Search
- Open the program file Sequential.java.
This class implements a sequential search. The class has two instance
variables. Variable list
represents the list to be examined. Variable comparisons
keeps track of the number of comparisons performed in the last search,
The class provides the following constructor and methods.
- Sequential(int[]
a)
- Uses a as the list to be
searched.
- search(int
key)
- Returns whether key is a
value in the list. If it is, then the index of the first occurrence is
returned; otherwise -1 is returned
- getComparisons()
- Returns the number
of element comparisons performed in the most recent search.
- Open program Searcher.java.
This program uses Sequential
to search through two lists, Both lists contains the same values.
However, the second list is sorted. The program prompts its user for a
key and uses Sequential
to search for the key. The program outputs the results of the search.
- Compile Sequential.java and
Searcher.java.
- Run the program six times
using as the key values 6, 11, 21, 22, 54, and 91. Record the number of searches the program reports as comments in Searcher.java.
- With randomly arranged data
and a random key value which is actually present in the list, generally
a program will examine approximately half the list to find the key
value. If the key value is not present, the entire list will be
examined. If we know instead that the data is already ordered (sorted),
then generally we can do much better, whether or not the key is present.
- How did sorting affect the
average number of comparisons? Probably not very much, because the
program has not been modified to take advantage of the sorting. A
significant advantage can be realized if we use the fact that the data
has been sorted. Let’s now modify the search class to achieve
this advantage.
- Because the data is sorted, we can stop searching for the key
value once we find a list value that is greater than the one for
which we are searching. Modify the Sequential class
to add a new public method
searchSorted() to terminate the search accordingly.
Essentially, this will be a copy of the search()
method, with the addition of code that will cause loop to terminate
(and the method to return -1) once the value in the list is greater
than the key value for which we are searching. Thus,
searchSorted() will have a single
int parameter named key. If
key is found in the list, then
searchSorted() should perform the same action as
search().
- Modify program Searcher.java
so that the initialization of index2
is performed using searchSorted() (line 23).
- Submit
the completed programs Sequential.java and Searcher.java.
Binary Search
The binary search described in
this section is a more efficient search than the modified sequential
search. For example, when you search for a name in a phone book, you
probably don’t start at the beginning and search straight
through. You further exploit the fact that the names are in sorted
order to speed up your search.
The binary search can be
implemented as an iterative technique in which each iteration
eliminates half of the remaining values from further consideration. The
search starts with the middle list element and decides which half of
the data to examine further. In the next iteration, the middle element
from the half of the data values that can possibly contain the key
value is determined, and from it, the range of possible positions for
the key value is restricted further. The process is repeated until the
search finds the key value or until the search determines that there
are no more potential positions to consider.
- Open and compile the class Binary.java.
The class provides a constructor and two methods
- Binary(int[]
a)
- Uses a as the list
to be searched. It is assumed that a is sorted.
- search(int
key)
- Performs a binary
search that returns whether key is a value in the list. If it is, then
the index of the first occurrence is returned; otherwise -1 is returned
- getComparisons()
- Returns the number
of element comparisons performed in the most recent search.
- Examine the method search()
in Binary.java to determine how the binary search algorithm is
implemented. In particular, notice that value mid is used as an index
of the middle element of the values currently being considered.
- Modify the BinarySearcher.java
program to use a Binary
object instead of a Sequential
object in the definition of s2.
- Run the program six times
using as the key values 6, 11, 21, 22, 54, and 91. Record the number of searches the program reports as comments in BinarySearcher.java.
- Submit
the completed program Binary.java and BinarySearcher.java.
As list size increases, the
efficiency improvement of binary search over sequential search is very
dramatic. For example binary search requires at most 20 comparisons for
a list of size 1000. Sequential search might make 1000 comparisons!
Finishing Up
- Copy any files you wish to
keep to your own drive.
- Delete all of the files you
copied or created on the
laboratory machine.
Homework J6
Use any remaining time in the
lab to work on HW
J6.