# Class 49: Merge Sort

Back to Insertion Sort. On to Project Assessment: Images.

This outline is also available in PDF.

Held: Monday, May 3, 2010

Summary: We continue our exploration of sorting by considering the applicability of divide-and-conquer to the problem of sorting. We look at one particular divide-and-conquer algorithm, merge sort. We explore how the running time for that algorithm varies based on the number of values we are sorting.

Related Pages:

Notes:

• For Tuesday and Wednesday, please review your classmates' work (URL distributed electronically).
• I'll reserve time at the start of class for questions on the examination.
• EC for Thursday's Convocation.
• EC for Sunday's Belly Dance performance (1:30 in Flanagan).

Overview:

• More efficient sorting techniques.
• Divide and conquer, revisited.
• Merge sort.
• Analyzing merge sort.

## Key Ideas of Merge Sort

• Divide and conquer is often a useful design strategy.
• For sorting, natural division is "first half" / "second half".
• What do we do after sorting the two halves? Merge 'em.

## An Alternate Implementation

• We can do something very much like merge sort while avoiding the splitting step.
• We start with a list of sorted lists, and repeatedly merge neighboring pairs.
• This technique is slightly easier to implement.
• This technique is much easier to analyze.

## The Costs of Merge Sort

• What's the running time? We do approximately N*log2N comparisons.
• The analysis:
• N steps to merge 2 sorted lists of length N/2
• N steps to merge 4 sorted lists of length N/4 into those two sorted lists.
• N steps to merge 8 sorted lists of length N/8 into those four sorted lists.
• And so on and so forth.
• Can we do better? Not if our sorting is based on comparing values to each other.

## Lab

Back to Insertion Sort. On to Project Assessment: Images.

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:16 2010.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2010S/Outlines/outline.49.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu