# Class 22: Searching

Back to Algorithm Analysis (2). On to Sorting.

Held: Thursday, 26 February 2004

Summary: Today we begin to discuss a key problem in Computer Science, searching. We also consider two algorithms commonly used to solve that problem.

Related Pages:

Assignments:

Notes:

• Sorry, grading delayed due to house construction.
• Don't forget pseudo-convo today.

Overview:

• Algorithms for common problems.
• A key problem: Searching.
• Sequential Search.
• Binary Search.
• Lab.

## Common Problems and Algorithms

• As we discussed early in the semester, a key aspect of computer science is the design of algorithms: formalized solutions to problems.
• There are a number of common problems for which computer scientists have developed common solutions.
• We'll visit two problems over the next few days: searching and sorting.
• As we develop algorithms, we'll consider intuitive ways that one might come up with the algorithms.

## Searching

• Goal: Find a value in a collection.
• Typically, the collection is linear: A vector or a list.
• Sometimes, the collection is also unordered. That is, there is no known arrangement to the list. For example, the books on the MathLan book shelves are not in an arrangement that would make it easy to search for a book with a particular title or by a particular author.
• For unordered collections, the typical search is sequential search, look at each element in turn.
• Sometimes, the collection is ordered. That is, the collection is organized by the primary key in which we search.
• For example, a phone book is ordered by name.
• Sequential search also works for ordered lists.
• However, we can also use something known as binary search:
• Look in the middle of the collection.
• If the middle is too small, anything smaller is also too small, so discard and try again.
• If the middle is too large, anything larger is also too alrge, so discard and try again.
• If the middle is just right, you're done.
• The analysis of binary search is comparatively easy:
• At each step, we divide the collection in half.
• When we reach one element, we're done.
• The number of steps is "the number of times you divide the collection in half to reach 1"
• As most of you know, that's log2(n)
• The implementation of binary search is a little bit more complicated: You need a way to quickly divide the collection in half.

## Lab

• Lab.
• We'll reflect tomorrow.

## History

Back to Algorithm Analysis (2). On to Sorting.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri May 7 09:43:15 2004.
The source to the document was last modified on Tue Jan 13 10:26:11 2004.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2004S/Outlines/outline.22.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu