[FrontPage] [TitleIndex] [WordIndex

Note: You are looking at a static copy of the former PineWiki site, used for class notes by James Aspnes from 2003 to 2012. Many mathematical formulas are broken, and there are likely to be other bugs as well. These will most likely not be fixed. You may be able to find more up-to-date versions of some of these notes at http://www.cs.yale.edu/homes/aspnes/#classes.

Mergesort is one of the canonical DivideAndConquer algorithms. Pseudocode can be found in almost any algorithms textbook (e.g. LevitinBook, page 124). Running time is given by the recurrence T(n) = 2T(n/2) + Theta(n), which has solution T(n) = Theta(n log n).

The usual implementation of merge requires Theta(n) extra space. It is possible to eliminate this overhead while retaining the O(n) running time for the merge. A description of one technique (citing Dokladi Akad. Nauk SSSR 186:1256-1258, 1969) is given as the answer to Exercise 5.2.4.18 on page 623 of volume 2 of KnuthSeries.

In practice QuickSort, which requires only O(log n) additional space and Theta(n log n) time on average, tends to be faster than MergeSort due to smaller constants. This guarantee is not absolute, however, because QuickSort is a randomized algorithm and may take as much as Theta(n2) time (with absurdly low probability).


CategoryAlgorithmNotes


2014-06-17 11:58