Laboratory 9

Array Searching

Week of 11 April, 2005


Home | Resources | Homeworks | Slides | Labs | Contacts | Submit | Forums | Private

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

 

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

 

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.

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

 

Homework J6

Use any remaining time in the lab to work on HW J6.