# Class 45: Sorting Techniques

Back to Searching Lab, Continued. On to Insertion Sort.

Held Monday, April 23, 2001

Summary

Today we revisit the problem of sorting. When you sort an array or vector, you put the elements in order (e.g., alphabetical, numerical, ...).

Notes:

• Don't forget to read the reading on sorting for Wednesday's class.
• Busy weekend.
• No example yet :-(.
• Exam 2 not yet graded.
• What topics have people come up with for homework 6?
• As I mentioned last week, Mr. Walker will be visiting class this week. Please don't let his presence change the way you act in class.

Overview

• The problem of sorting, revisited
• Formalizing the problem
• Writing sorting algorithms
• Examples
• Insertion sort
• Selection sort
• Quick sort
• Merge sort

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

## Sorting Examples

• In my experience, most of us know some sorting algorithms, but have difficulty generalizing them.
• I'll bring in some things to sort (most frequently, a stack of CDs) and we'll look at different ways to sort them.

## Formal Description

• Before moving on to algorithms for solving the sorting problem, let's take a look at the way wemight 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.

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

## Insertion Sort

• One simple sorting technique is insertion sort. Insertion sort is the sort Peter sugested.
• 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 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.
```+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+
|
Unsorted           Sorted
```
• Note that we can also write selection sort iteratively.
• One of you (Natalia? Audrey?) suggested we separate our pile into big and small elements, sort the two subpiles, and combine them.
• That's a great idea, but it can be hard to decide what big and small mean.
• I'll show examples with a list of numbers.
• Because of the complexity of choosing how to split, we'll leave Quick Sort to CSC152 and CSC301.
• One of you (me? Audrey?) suggested that we simply split the pile in two and sort the two halves.
• Sarah (I think) suggested that we would then have trouble merging the halves.
• So, let's think again about what we can do if we've sorted the two halves. How hard is it to put them together again?

## History

Friday, 12 January 2001

• Created generic outline format for class.

Back to Searching Lab, Continued. On to Insertion Sort.

Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This page was generated by Siteweaver on Wed May 5 12:15:11 2004.
This page may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/outline.45.html`.