# Class 47: Introduction to Sorting

Back to Binary Search. On to Insertion Sort.

This outline is also available in PDF.

Held: Wednesday, April 28, 2010

Summary: We explore the problem of sorting. When you sort a list, vector, or other collection, you put the elements in order. The order of the elements usually corresponds to the type of the elements. We might sort strings alphabetically, grades numerically, colors by brightness, and so on and so forth.

Related Pages:

Notes:

• Reading for Friday: Sorting.
• I will not be in class on Friday; Prof. Weinman and a guest will be.
• EC for Osgood convocation tomorrow.
• EC for Friday's CS Extra Table (Science 3821). I need a headcount!

Overview:

• The problem of sorting.
• Writing sorting algorithms.
• Examples: Insertion, selection, etc.
• Formalizing the problem.

## The Problem of Sorting

• As we saw recently, one problem that seems to crop up a lot in programmming (and elsewhere) is that of sorting.
• The problem: Given a list, array, vector, sequence, or file of comparable elements, put the elements in order.
• In order typically means that each element is no bigger than the next element. (You can also sort in decreasing order, in which case each element is no smaller than the next element.)
• We'll look at techniques for sorting vectors and lists.

## Designing Sorting Algorithms

• I suggest that you think about the development of sorting algorithms in Scheme similarly to the way you think about writing many algorithms.
• Start by thinking about the way you might do it by hand.
• We may find a few different ways to sort by hand.
• We'll probably leave the Scheme-ification to the end.
• Generalize what you're doing.
• What is the philosophy of your techinque?
• What are the key steps.
• Come up with some initial test cases.
• Consider whether there are any deficiences to your technique.
• Decide on your parameters (e.g., are you sorting a list or a vector).
• Translate your algorithm into Scheme.
• Test test test.

## Sample Sorting Algorithms

### Insertion Sort

• One simple sorting technique is insertion sort.
• Insertion sort operates by segmenting the list into unsorted and sorted portions, and repeatedly removing the first element from the unsorted portion and inserting it into the correct place in the sorted portion.
• This may be likened to the way typical card players sort their hands.
• How might we code this recursively?
• Does our code differ for lists and arrays?

### Selection Sort

• Selection sort is among the simpler and more natural methods for sorting vectors.
• In this sorting algorithm, you segment the vector into two subparts, a sorted part and an unsorted part. You repeatedly find the largest of the unsorted elements, and swap it into the beginning of the sorted part. This swapping continues until there are no unsorted elements.
• Here's a potentially-helpful picture:
```+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+
|
Unsorted           Sorted
```
• Note that we can also write selection sort iteratively.

## A More Formal Description

• Before moving on to algorithms for solving the sorting problem, let's take a look at the way we might document one (or all) of the procedures
• Purpose?
• Parameters?
• Produces?
• Preconditions?
• Postconditions?
• Here are some postconditions I typically think about:
• You also need to ensure that all elements in the original list are in the sorted list.
• You also need to ensure that no other elements are in the list.

Back to Binary Search. On to Insertion Sort.

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 11 09:03:16 2010.
The source to the document was last modified on Thu Jan 21 13:05:12 2010.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2010S/Outlines/outline.47.html`.

You may wish to validate this document's HTML ; ;

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright © 2007-10 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.) This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit `http://creativecommons.org/licenses/by-nc/2.5/` or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.