# Class 43: Basics of Searching

Back to Association Lists. On to Binary Search.

Held: Monday, 17 November 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:

• Are there any questions on the lab writeup?

Overview:

• Algorithms for common problems.
• Two key problems: Searching and Sorting.
• Searching Examples.
• Sequential Search.
• Binary Search.
• Searching in Scheme.

## 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 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 sorted. That is, the collection is organized by the primary key in which we search.
• For example, a phone book is sorted by name.
• Sequential search also works for sorted 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 large, so discard and try again.
• If the middle is just right, you're done.

## Searching in Scheme

• What parameters might we give to a searching algorithm in Scheme?
• Certainly the list or vector to search.
• What else?
• The value to search for?
• A predicate that tells us whether or not the thing matches?
• The value to search for and a way of comparing values?
• Anything else?

## History

Thursday, 28 August 2003 [Samuel A. Rebelsky]

• Created automatically.

Back to Association Lists. On to Binary Search.

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 Dec 9 13:59:57 2003.
The source to the document was last modified on Mon Sep 1 13:30:51 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Outlines/outline.43.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu