Chapter 10
Searching and Sorting
In this chapter we will look at two closely related problems: searching and sorting. Both
problems appear in applications involving databases, and without efficient solutions to
these problems databases would be virtually useless. They would be useless because
databases that are large enough to hold the information we need would be too large to
use. They would be too large to use because it would take too long to find what we are
looking for inside them. There is a large variety of specific applications of searching and
sorting, but the most basic ones involve locating an element in a vector whose value is
equal to a target value. We will use that simple problem as our driving example
throughout this chapter.
10.1 Searching
Searching is important in many areas of computer science. Many large computer systems
include databases that must be searched when an inquiry is made. For example, when you
phone a creditcard company to inquire about your bill, you will be asked for your card
number. In a few seconds the company representative will be looking at your data. Your
data and that of tens of millions of other customers are all stored in a database. Your
information is found by searching among all those millions of card numbers. The target
of the search is your unique number. Furthermore, every day, millions of transactions,
each involving one of those unique numbers take place, and each one requires that a
target number be located in a list of all card numbers. In each of these examples, in the
calculation of account balances, and many other cases, the first task it to find the account
with the target number, and it is accomplished by a searching algorithm.
10.1.1 Sequential search
The simplest search method is the
sequential search
method, also known as the
linear
search
method. In this method the target value that is being sought is compared with the
first member of the database, then with the second, etc., until either the number is found,
or the end of the list is reached, whichever comes first. Instead of dealing with a real
database, let’s work on the simplest version of the search problem. We are given a vector
of numbers and we are given a target value. Our task is to search for that target value in
the vector. If we find it, then the answer we give is the index of the element that we
found.
If we do not find it, we return an impossible index, say, 1, as a
flag
indicating that the
search failed. That flag is crucial to the search algorithm because there must be some
means of informing the searcher that the target is not there. The term “flag” is a special
term in computer science, and it is used in many contexts. It means a value indicating a
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
special condition. In this case the condition is “target not found”, or “search failure”. In
other situations it might mean “modem is ready to receive data” (
i.e.
, good news) or
“modem is not ready to receive data” or “buffer empty” or “disk full”,
etc.
A given flag
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '07
 FITZPATRICK
 Computer Science, matlab, Binary Search, Databases, Sort

Click to edit the document details