<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss'><id>tag:blogger.com,1999:blog-32272696</id><updated>2009-02-21T06:47:04.405-08:00</updated><title type='text'>Merge</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://i-merge.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/32272696/posts/default'/><link rel='alternate' type='text/html' href='http://i-merge.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>GYK</name><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-32272696.post-115486492447975818</id><published>2006-08-06T04:42:00.000-07:00</published><updated>2006-08-06T04:55:39.926-07:00</updated><title type='text'>Merge sort</title><content type='html'>&lt;div align="justify"&gt;In computer science, merge sort or mergesort is a sorting 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. It is a comparison sort.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Algorithm&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;Conceptually, merge sort works as follows:&lt;br /&gt;Divide the unsorted list into two sublists of about half the size&lt;br /&gt;Sort each of the two sublists&lt;br /&gt;Merge the two sorted sublists back into one sorted list.&lt;br /&gt;The algorithm was invented by John von Neumann in 1945.&lt;br /&gt;In a simple pseudocode form, the algorithm could look something like this:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;em&gt;function mergesort(m)&lt;br /&gt;var list left, right&lt;br /&gt;if length(m) ≤ 1&lt;br /&gt;return m&lt;br /&gt;else&lt;br /&gt;middle = length(m) / 2&lt;br /&gt;for each x in m up to middle&lt;br /&gt;add x to left&lt;br /&gt;for each x in m after middle&lt;br /&gt;add x to right&lt;br /&gt;left = mergesort(left)&lt;br /&gt;right = mergesort(right)&lt;br /&gt;result = merge(left, right)&lt;br /&gt;return result&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;There are several variants for the merge() function, the simplest variant could look like this:&lt;br /&gt;&lt;br /&gt;&lt;em&gt;function merge(left,right)&lt;br /&gt;var list result&lt;br /&gt;while length(left) &gt; 0 and length(right) &gt; 0&lt;br /&gt;if first(left) ≤ first(right)&lt;br /&gt;append first(left) to result&lt;br /&gt;left = rest(left)&lt;br /&gt;else&lt;br /&gt;append first(right) to result&lt;br /&gt;right = rest(right)&lt;br /&gt;if length(left) &gt; 0&lt;br /&gt;append left to result&lt;br /&gt;if length(right) &gt; 0&lt;br /&gt;append right to result&lt;br /&gt;return result&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;&lt;strong&gt;Analysis&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;In sorting n items, merge sort has an average and worst-case performance of O(n log n). If the running time of merge sort for a list of length n is T(n), then the recurrence T(n) = 2T(n/2) + n follows from the definition of the algorithm (apply the algorithm to two lists of half the size of the original list, and add the n steps taken to merge the resulting two lists). The closed form follows from the master theorem.&lt;br /&gt;In the worst case, merge sort does exactly (n ⌈log n⌉ - 2⌈log n⌉ + 1) comparisons, which is between (n log n - n + 1) and(n log n - 0.9139·n + 1) [logs are base 2]. Note, the worst case number given here does not agree with that given in Knuth's Art of Computer Programming, Vol 3. The discrepancy is due to Knuth analyzing a variant implementation of merge sort that is slightly sub-optimal.&lt;br /&gt;For large n and a randomly ordered input list, merge sort's expected (average) number of comparisons approaches α·n fewer than the worst case, where&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;img class="tex" alt="\alpha = -1 + \sum_{k=0}^\infty \frac1{2^k+1} \approx 0.2645" src="http://upload.wikimedia.org/math/9/9/7/997a1d37b1ff21afbad73eaf299d1521.png" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div align="justify"&gt;&lt;/div&gt;&lt;div align="justify"&gt;&lt;/div&gt;&lt;div align="justify"&gt;&lt;/div&gt;&lt;div align="justify"&gt;&lt;/div&gt;&lt;div align="justify"&gt;In the worst case, merge sort does about 39% fewer comparisons than quicksort does in the average case; merge sort always makes fewer comparisons than quicksort, except in extremely rare cases, when they tie, where merge sort's worst case is found simultaneously with quicksort's best case. 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 many iterations as the worst case.&lt;br /&gt;Recursive implementations of merge sort make 2n - 1 method calls in the worst case, compared to quicksort's n, thus has roughly twice as much recursive overhead as quicksort. However, iterative, non-recursive, implementations of merge sort, avoiding method call overhead, are not difficult to code. Merge sort's most common implementation does not sort in place, therefore, the memory size of the input must be allocated for the sorted output to be stored in. Sorting in-place is possible (see, for example, [1]) but is very complicated, and will offer little performance gains in practice, even if the algorithm runs in O(n log n) time. In these cases, algorithms like heapsort usually offer comparable speed, and are far less complex.&lt;br /&gt;Merge sort is 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 (inefficient) implementations of quicksort, merge sort is a stable sort as long as the merge operation is implemented properly&lt;/div&gt;&lt;div align="justify"&gt;&lt;/div&gt;&lt;div align="justify"&gt;&lt;/div&gt;&lt;div align="justify"&gt; &lt;/div&gt;&lt;div align="justify"&gt;&lt;strong&gt;Merge sorting tape drives&lt;/strong&gt;&lt;/div&gt;&lt;div align="justify"&gt; &lt;/div&gt;&lt;div align="justify"&gt;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:&lt;/div&gt;&lt;div align="justify"&gt;&lt;br /&gt;divide the data to be sorted in half and put half on each of two tapes&lt;br /&gt;merge individual pairs of records from the two tapes; write two-record chunks alternately to each of the two output tapes&lt;br /&gt;merge the two-record chunks from the two output tapes into four-record chunks; write these alternately to the original two input tapes&lt;br /&gt;merge the four-record chunks into eight-record chunks; write these alternately to the original two output tapes&lt;br /&gt;repeat until you have one chunk containing all the data, sorted --- that is, for log n passes, where n is the number of records. &lt;/div&gt;&lt;div align="justify"&gt;&lt;br /&gt;For the same reason it is also very useful for sorting data on disk that is too large to fit entirely into primary memory. On tape drives that can run both backwards and forwards, you can run merge passes in both directions, avoiding rewind time.&lt;/div&gt;&lt;div align="justify"&gt; &lt;/div&gt;&lt;div align="justify"&gt; &lt;/div&gt;&lt;div align="justify"&gt; &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/32272696-115486492447975818?l=i-merge.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/32272696/posts/default/115486492447975818'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/32272696/posts/default/115486492447975818'/><link rel='alternate' type='text/html' href='http://i-merge.blogspot.com/2006/08/merge-sort.html' title='Merge sort'/><author><name>GYK</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='13905710167647881464'/></author></entry></feed>