# Class 48: Merge Sort

Back to Insertion Sort. On to Objects in Scheme.

This outline is also available in PDF.

Held: Wednesday, 25 November 2009

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:

• No reading for Monday. (I may distribute links to the project images so that we can better discuss project code on Monday.)
• I hope you have a wonderful Thanksgiving!

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 Objects in Scheme.

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 Fri Dec 11 09:38:52 2009.
The source to the document was last modified on Fri Aug 21 17:03:06 2009.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2009F/Outlines/outline.48.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright © 2007-9 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.) This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit `http://creativecommons.org/licenses/by-nc/2.5/` or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.