# Class 24: Fast Sorting Algorithms

Back to Sorting. On to Sorting Laboratory.

Held: Friday, 28 February 2003

Summary: Today we consider two important O(nlog2n) algorithms, merge sort and Quicksort.

Related Pages:

Assignments:

Notes:

• Are there questions on homework 2?
• What preconditions and postconditions did you set for `insert`?

Overview:

• Formalizing the problem.
• Merge sort.
• Quicksort.
• Lab.

## Faster Sorting Algorithms

• We saw in the problem of searching that it helps to apply the divide-and-conquer technique.
• That technique helped us design an O(log2n) searching algorithm.
• Can we apply the same technique to write a faster sorting algorithm?
• We'll consider two possible variants.

### Merge Sort

• We could simply split the pile into two equal halves. in two and sort the two halves.
• We then need to merge the two 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?

### Quicksort

• One idea is to split the pile into two piles, one of big elements and one of 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.

## Lab

• You can explore these issues in more depth in the lab.
• Be prepared to reflect.

## History

Thursday, 15 January 2003 [Samuel A. Rebelsky]

• Created as a mostly-blank outline.

Thursday, 24 February 2003 [Samuel A. Rebelsky]

• Filled in the details.

Back to Sorting. On to Sorting Laboratory.

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 6 09:20:40 2003.
The source to the document was last modified on Thu Feb 27 21:53:34 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.24.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu