CPS1 Review: 
Sorting and Searching


Sorting

Sorting is the process of ordering a collection according to some value.  For example, phone books are sorted by last name.  Sorting algorithms are important, partly because a sorted collection makes searching much more efficient, as we will see.  The question, then, is how to sort a collection to begin with.  There are hundreds of sorting algorithms, and there is always research on how to sort faster given the problem situation.  There is no one best sorting algorithm.  The best sorting algorithm depends on the problem at hand: the elements of the collection, the value that they are sorted by, and the way that the collection is stored.

Ways to Sort Things

Searching

For this review, we will call each thing in the collection an element, and the thing we are looking for the target.

Searching is the process of looking for a target in a collection of possibilities, such as looking up a name in a phone book.

Ways to Search

There are several ways to search a collection.  You can start at the front and look at each element to see if it is the target.  For example, you could look up a name in the phone book by starting at the first name and looking at each one until you find the name you want.  This is called a sequential search.  You can also start at the back and work your way to the front, one element at a time.  This is a backward sequential search.

If your list is ordered, you can search in a smarter way.  For example, suppose I have a secret number between 1 and 100.  You are trying to guess my number in the shortest amount of time possible, and after each of your guesses, I will tell you higher or lower.  What will your first guess be?  What will your second guess be?

Most people would have 50 be their first guess, and then either 25 or 75 depending on if I said higher or lower.  Why is this a good strategy?  This is a good strategy because after the first guess one half of the possibilities are eliminated.  If your first guess is 1, and that is not the secret number, then I say "higher" and there are still 99 possibilities for my number.

This kind of search is called a binary search.  It depends on the collection being sorted (numbers are naturally ordered from least to greatest so the number game fits this criteria) and on the fact that I can compare an element of the collection to tell if it is higher, lower, or equal to the target (in the number game, I say "higher or lower").  We did a binary search through a phone book in class by looking a the middle, figuring out which half the name must be in, throwing away the other half, and starting over with the relevant half.

Analysis

To search sequentially through n elements, one must compare the target with at most all n elements.

To do a binary search through n elements, one must compare the target with at most lg n elements.  However, more processing must have been done to begin with in order to use binary search.