Back to Pause for Breath. On to Faster Sorts.

Held: Friday, April 28, 2006

Summary: Today we consider two of the common quadratic sorting methods: selection sort and insertion sort.

Related Pages:

Assignments

• Exam 3 distributed (due a week from Monday).

Notes:

• Exam 2 returned. Discussion next Tuesday.
• Some changes to the schedule.

Overview:

• Review.
• Bubble sort, and why not to use it.
• Selection sort.
• Insertion sort.

Review

• Sorting involves putting values "in order".
• Many dimensions. We've chosen
• Sort Vectors
• Using a Comparator to deterine order
• Returning a newly-created Vector
• Without guaranteeing stability
• Testing can be automated.

Bubble Sort

• Bubble sort is bad. Don't use it.

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.
```+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+
|
Unsorted           Sorted
```
• Note that we can write selection sort both recursively and iteratively.
• We can also write selection sort for lists (although "swapping" can be harder).
• Is this sort stable?
• What is the running time?

Insertion Sort

• Another simple sorting technique is insertion sort.
• Insertion sort operates by segmenting the vector 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.
• The diagram is somewhat reversed from selection sort.
```+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+
|
Sorted             Unsorted
```
• This may be likened to the way typical card players sort their hands.
• For vectors, inserting often requires shifting elements until we have a space at the correct place.
• Since we have to keep track of the value being inserted, we swap with neighbors.
• For example
```           SORTED       |  UNSORTED
+---+---+---+---+---+---+---+---+
| 1 | 3 | 6 | 8 | 9 | 2 | . | . |
+---+---+---+---+---+---+---+---+

Inserting 2
+---+---+---+---+---+---+---+---+
| 1 | 3 | 6 | 8 | 9 | 2 | . | . |
+---+---+---+---+---+---+---+---+
*
Swap
+---+---+---+---+---+---+---+---+
| 1 | 3 | 6 | 8 | 2 | 9 | . | . |
+---+---+---+---+---+---+---+---+
*

Swap
+---+---+---+---+---+---+---+---+
| 1 | 3 | 6 | 2 | 8 | 9 | . | . |
+---+---+---+---+---+---+---+---+
*

Swap
+---+---+---+---+---+---+---+---+
| 1 | 3 | 2 | 6 | 8 | 9 | . | . |
+---+---+---+---+---+---+---+---+
*

Swap
+---+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 6 | 8 | 9 | . | . |
+---+---+---+---+---+---+---+---+
*

Done
SORTED         | UNSORTED
+---+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 6 | 8 | 9 | . | . |
+---+---+---+---+---+---+---+---+
*
```
• We can now insert the value at the next position
```    +---+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 6 | 8 | 9 | 7 | . |
+---+---+---+---+---+---+---+---+
*
+---+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 6 | 8 | 7 | 9 | . |
+---+---+---+---+---+---+---+---+
*
+---+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 6 | 7 | 8 | 9 | . |
+---+---+---+---+---+---+---+---+
*
Done                            |
+---+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 6 | 7 | 8 | 9 | . |
+---+---+---+---+---+---+---+---+
```
• How does our code differ for lists and arrays?

Back to Pause for Breath. On to Faster Sorts.

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 9 08:31:55 2006.
The source to the document was last modified on Thu Jan 12 14:58:07 2006.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2006S/Outlines/outline.48.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu