Merge sort
From Academic Kids

In computer science, merge sort or mergesort is a sort algorithm for rearranging lists (or any other data structure that can only be accessed sequentially, e.g. file streams) into a specified order. It is a particularly good example of the divide and conquer algorithmic paradigm.
Contents 
Algorithm
Conceptually, merge sort works as follows :
 Divide the unsorted list into two sublists of about half the size
 Sort each of the two sublists
 Merge the two sorted sublists back into one sorted list.
The algorithm was invented by John von Neumann in 1945.
Analysis
Mergesort has an average and worstcase performance of O(n log n). In the worst case, merge sort does about 30% fewer comparisons than quicksort does in the average case; thus merge sort very often needs to make fewer comparisons than quicksort. In terms of moves, merge sort's worst case complexity is O(n log n)the same complexity as quicksort's best case, and merge sort's best case takes about half as much time as the worst case.
However, merge sort performs 2n  1 method calls in the worst case, compared to quicksort's n, thus has roughly twice as much recursive overhead as quicksort. Mergesort's most common implementation does not sort in place, meaning memory the size of the input must be allocated for the sorted output to be stored in. Sorting inplace is possible but requires an extremely complicated implementation.
Although merge sort can sort linked lists, it is also much more efficient than quicksort if the data to be sorted can only be efficiently accessed sequentially, and is thus popular in languages, such as Lisp, where sequentially accessed data structures are very common. Unlike some optimized implementations of quicksort, merge sort is a stable sort, as long as the merge operation is implemented properly.
More precisely, merge sort does between ⌈ n log n  n + 1 ⌉ and ⌈ n log n  0.914·n ⌉ comparisons in the worst case.
Merge sorting tape drives
Merge sort is so inherently sequential that it's practical to run it using slow tape drives as input and output devices. It requires very little memory, and the memory required does not change with the number of data elements. If you have four tape drives, it works as follows:
 divide the data to be sorted in half and put half on each of two tapes
 merge individual pairs of records from the two tapes; write tworecord chunks alternately to each of the two output tapes
 merge the tworecord chunks from the two output tapes into fourrecord chunks; write these alternately to the original two input tapes
 merge the fourrecord chunks into eightrecord chunks; write these alternately to the original two output tapes
 repeat until you have one chunk containing all the data, sorted  that is, for log n passes, where n is the number of records.
On tape drives that can run both backwards and forwards, you can run merge passes in both directions, avoiding rewind time. For the same reason it is also very useful for sorting data on disk that is too large to fit entirely into primary memory.
Optimizing merge sort
This might seem to be of historical interest only, but on modern computers, locality of reference is of paramount importance in software optimization, because multilevel memory hierarchies are used. In some sense, main RAM can be seen as a slow tape drive, level 3 cache memory as a slightly faster one, level 2 cache memory as faster still, and so on. In some circumstances, cache reloading might impose unacceptable overhead and a carefully crafted merge sort might result in a significant improvement in running time. This opportunity might change if fast memory becomes very cheap again, or if exotic architectures like the Tera MTA become commonplace.
Designing a merge sort to perform optimally often requires adjustment to available hardware, eg. number of tape drives, or size and speed of the relevant cache memory levels.
Comparison with other sort algorithms
Although heap sort has the same time bounds as merge sort, it requires only Ω(1) auxiliary space instead of merge sort's Ω(n), and is consequently often faster in practical implementations. Quicksort, however, is considered by many to be the fastest generalpurpose sort algorithm in practice. Its averagecase complexity is O(n log n), with a much smaller coefficient, in good implementations, than merge sort's, even though it is quadratic in the worst case. On the plus side, merge sort is a stable sort, parallelizes better, and is more efficient at handling slowtoaccess sequential media. Merge sort is often the best choice for sorting a linked list: in this situation it is relatively easy to implement a merge sort in such a way that it does not require Ω(n) auxiliary space (instead only Ω(1)), and the slow randomaccess performance of a linked list makes some other algorithms (such as quick sort) perform poorly.
As of Perl 5.8, merge sort is its default sorting algorithm (it was quicksort in previous versions of Perl).
Sample implementations
See the Merge algorithm article for the respective implementations of the merge
functions referenced in the examples below.
Haskell
sort :: Ord a => [a] > [a] sort [] = [] sort [x] = [x] sort xs = merge (sort ys) (sort zs) where (ys,zs) = splitAt (length xs `div` 2) xs
Python
def sort(array): if len(array) <= 1: return array mid = len(array) // 2 return merge (sort(array[0:mid]), sort(array[mid:]))
Ruby
def sort(array) mid = array.length / 2 mid < 1 ? array : merge(sort(array[0...mid]), sort(array[mid..1])) end
Miranda
sort [] = [] sort [x] = [x] sort array = merge (sort left) (sort right) where left = [array!y  y < [0..mid]] right = [array!y  y < [(mid+1)..max]] max = #array  1 mid = max div 2
C / C++ / Java
void Sort(float array[], int begin, int end) { int mid; if (begin == end) return; if (begin == end  1) return; mid = (begin + end) / 2; Sort(array, begin, mid); Sort(array, mid, end); Merge(array, begin, mid, end); }
External links
 Dictionary of Algorithms and Data Structures: Merge sort (http://www.nist.gov/dads/HTML/mergesort.html)
 Power Programming  Merge Sort (http://www.bitesizeinc.net/power.program.merge.sort.html)
 Merge Sort Algorithm Simulation (http://www.geocities.com/SiliconValley/Program/2864/File/Merge1/mergesort.html)
 Mergesort For Linked Lists (http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html)de:Mergesort
es:Merge sort fr:Tri fusion he:מיון מיזוג lt:Sąlajos rūšiavimas nl:Mergesort ja:マージソート pl:Mergesort pt:Merge sort fi:Lomituslajittelu zh:归并排序