# Class 43: Basics of Searching and Sorting

Back to Tail Recursion (2). On to Binary Search.

Held: Wednesday, 16 April 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 questions on the project?
• Extra credit for attending today's cool talk at 4:15.
• Amanda P. has graciously helped me prepare a list of possible forms of extra credit. Let me know if you have updates.
• I will be absent from class on Monday. We'll be doing film day in 151. Reese will bring treats.

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 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.

## 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, 16 January 2003 [Samuel A. Rebelsky]

• First version, created mostly automatically from previous course.

Back to Tail Recursion (2). 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 May 6 09:30:15 2003.
The source to the document was last modified on Thu Jan 16 13:46:00 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Outlines/outline.43.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu