# Class 22: Searching

Back to Tail Recursion. On to Sorting.

Held: Tuesday, 25 February 2003

Summary: Today we begin to discuss two key problems in Computer Science, searching and sorting. We also consider algorithms commonly used to solve those problems.

Related Pages:

Assignments:

Notes:

• I understand that some of you were dismayed that I spent an hour telling you what was wrong with your exams, in spite of your general success on the exam. I'm proud of how well you did. I just consider it my responsibility to push you to do even better.
• I know some of you are unhappy with my grading policies for extra credit. I'll try to explain my rationale again.
• Are there questions on homework 2?

Overview:

• Algorithms for common problems.
• A key problem: Searching.
• Searching Examples.
• 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.

## Lab

• Lab.
• We'll reflect tomorrow.

## History

Thursday, 15 January 2003 [Samuel A. Rebelsky]

• Created as a mostly-blank outline.

Monday, 24 February 2003 [Samuel A. Rebelsky]

• Updated. Added introductory material.

Back to Tail Recursion. 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 Tue May 6 09:20:38 2003.
The source to the document was last modified on Mon Feb 24 22:35:15 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.22.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu