# Class 45: Quadratic Sorts

Back to An Introduction to Sorting. On to O(nlogn) Sorts.

Held: Friday, 19 November 2004

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

Related Pages:

Assignments

Overview:

• Testing sorting, continued
• Selection sort
• Insertion sort

## Testing, Continued

• We've written one simple test.
• What others might we write?
• What else should we do?
• We certainly need to ensure that the sorting preserves elements.
• One that prints a result rather than returning succeeded/failed.
• One that starts with a sorted vector.
• One that starts with a "backwards-sorted" vector.
• One that starts with a "randomly permuted" vector.
• Different sizes.

## 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 write selection sort both recursively and iteratively.
• We can also write selection sort for lists (although "swapping" can be harder).

## Insertion Sort

• One 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
```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
```
• 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 | 7 | . |
+---+---+---+---+---+---+---+---+
*
```
• How does our code differ for lists and arrays?

Back to An Introduction to Sorting. On to O(nlogn) 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 Wed Dec 8 10:37:27 2004.
The source to the document was last modified on Thu Aug 26 20:22:24 2004.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2004F/Outlines/outline.45.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu