Class 43: Basics of Searching and Sorting

Back to Discussion of Exam 2. On to Searching: Laboratory.

Held Tuesday, November 19, 2002

Summary

Today we begin to discuss two key problems, searching and sorting, and the algorithms commonly used to solve those problems.

Notes

• Final reminder: Go to Holly's talk at noon today and receive extra credit plus free pizza and cake.
• You might even learn something.
• I'll be absent on Friday. I was going to have you come in for class, but I've decided that if I get the day off, you should, too.
• Are there more questions on homework 8?
• Here's a definition of `right-insert` (which you should remember from homework 6: Given a binary procedure, binproc, and a list of values, lst `(insert-right binproc lst)`, inserts binproc between each pair of values, grouping to the right. E.g.
```(insert-right - (list 1 2 3 4 5))
=> (- 1 (- 2 (- 3 (- 4 5))))

(define foo (lambda (x y) (+ (* 10 x) y)))
(insert-right foo (list 1 2 3 4))
=> (foo 1 (foo 2 (foo 3 4)))
```

Overview

• Algorithms for common problems
• Two key problems: Searching and Sorting
• Searching Examples
• Sorting Examples

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: An array 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.

Sorting

• Given an unordered list, how do we make it ordered? That's the problem of sorting, which we'll return to early next week.
• We may do one example, though.

History

Thursday, 29 August 2002

Back to Discussion of Exam 2. On to Searching: Laboratory.

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 Mon Dec 2 08:42:00 2002.
The source to the document was last modified on Tue Sep 3 23:13:33 2002.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Outlines/outline.43.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu