S
EARCHING
Contents
z
Introduction
z
Searching in an Array
z
The Comparable Interface
Introduction
Searching is a very common problem, both in life and in computer applications. It is so important
that many data structures and algorithms have been designed specifically to make searching as easy
and efficient as possible.
In this set of notes we will review the three techniques that we discussed the first day of class for
searching for a value in an array. Other sets of notes will present several different data structures
designed to support efficient searches.
Searching in an Array
Recall that there are two basic approaches to searching for a given value
v
in an array:
sequential
search
and
binary search
.
Sequential search involves looking at each value in turn (i.e., start with the value in array[0], then
array[1], etc). The algorithm quits and returns true if the current value is v; it quits and returns false
if it has looked at all of the values in the array without finding v.
If the values are in
sorted
order, then the algorithm can sometimes quit and return false without
having to look at all of the values in the array; in particular, if the current value is
greater
than v
then there is no point in looking at the rest of the values in the array (v is definitely not there).
The worstcase time for a sequential search in an array of size N is always O(N), even when the
array is sorted (though the averagecase time should be better for a sorted array than for a nonsorted
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 MarvinSolomon
 Data Structures

Click to edit the document details