Content deleted Content added
No edit summary Tag: references removed |
|||
Line 1:
{{orphan|date=November 2015}}
In computer science,
== 2-Way Merge ==
A 2-Way Merge, or a binary merge, has been studied extensively due to its key role in [[Merge sort]]. An example of such is the classic merge that appears frequently in merge sort examples. The classic merge outputs the data item with the lowest key at each step; given some sorted lists, it produces a sorted list containing all the elements in any of the input lists, and it does so in time proportional to the sum of the lengths of the input lists. There are algorithms that exist that can operate in better than linear times such as the Hwang-Lin Merging Algorithm.<ref>F.K.Hwang and S. Lin, \A Simple Algorithm for Merging Two Disjoint Linearly Ordered Sets", SIAM Journal on Computing 1 (1972), 31-39.</ref>
Denote by A[1..p] and B[1..q] two arrays sorted in increasing order.
Further, denote by C[1..n] the output array.
Initially, these indices refer to the first element, i.e., are 1.
If A[i] < B[j], then the algorithm copies A[i] into C[k] and increases i and k.
Otherwise, the algorithm copies B[j] into C[k] and increases i and k.
A special case arises if either i or j have reached the end of A or B.
In this case the algorithm copies the remaining elements of B or A into C and terminates.
The K-way merge problem consists of merging k sorted arrays to produce a single sorted array with the same elements.
Denote by n the total number of elements.
For simplicity, we assume that none of the input arrays is empty.
As a consequence k < n, which simplifies the reported running times.
The problem can be solved in O(n log k) running time with O(n) space.
Several algorithms that achieve this running time exist.
===
The problem can be merged by iteratively merging two of the k arrays using a 2-Way Merge until only a single array is left.
If the arrays are merged in arbitrary order, then the resulting running time is only O(kn).
This is suboptimal.
The running time can be improved by iteratively merging the first with the second, the third with the fourth, and so one.
==== Non-optimal ====▼
As the number of arrays is halved in each iteration, there are only Θ(log k) iteration.
In each iteration every element is moved exactly once.
The running time per iteration is therefore in Θ(n) as n is the number of elements.
The total running time is therefore in Θ(n log k).
We can further improve upon this algorithm, by iteratively merging the two shortest arrays.
It is clear that this minimizes the running time and can therefore not be worse than the stratedy described in the previous paragraph.
The running time is therefore in O(n log k).
Fortunately, in border cases the running time can be better.
Consider for example the degenerate case, where all but one array contain only one element.
The stratedy explained in the previous paragraph needs Θ(n log k) running time, while the improved one only needs Θ(n) running time
=== Direct K-Way Merge ===
The basic idea of a direct K-Way Merge consists of efficiently computing the minimum element of all k arrays and then transfering it to the output array.
▲== Ideal Merge ==
A straight-forward implementation would scan all k arrays to determine the minimum.
This straight-forward implementation results in a running time of Θ(kn).
We can improve upon this by computing the smallest element faster.
# Each list is sorted by the value of its head element▼
By using either [[Heap (data structure)|heaps]] or tournament trees the smallest element can be determined in O(log k) time.
The resulting running times are therefore in O(n log k).
The heap algorithm allocates a min-heap of pointers into the input arrays.
Initially these pointers point to the smallest elements of the input array.
In a O(k) preprocessing step the heap is created using the standard heapify procedure.
Afterwards, the algorithm iteratively transfers the element that the root pointer points to, increases this pointer and executes the standard increase key procedure upon the root element.
The running time of the increase key procedure is bounded by O(log k).
As there are n elements, the total running time is O(n log k).
==== Tournament Tree ====
On the other hand, if the sorted items in a [[Heap (data structure)|heap]], then the running time becomes Θ(n log k).▼
The tournament tree<ref>
Every leaf stores a pointer into one of the input arrays.
Further, for every node in the tree, a value and an index is stored.
For the k-th leaf the value is the value that its pointer points to and the index is k.
For every internal leaf the value is minimum of the values of the node's children.
The index of an internal leaf indicates which input array the value comes from.
The root of the tournament tree contains the smallest element and the index of the corresponding input array.
The algorithm iteratively transfers this element and advances the corresponding pointer.
It then updates the values of the nodes on the path from the corresponding leaf to the root.
As the tree is balanced, this path contains only Θ(log k) element.
As there n elements that need to be extracted, the resulting total running time is Θ(n log k).
▲
===== Example =====
As an example, let's say we have 4 sorted arrays:<ref>{{Cite web|title = kway - Very fast merge sort - Google Project Hosting|url = https://code.google.com/p/kway/|website = code.google.com|accessdate = 2015-11-22}}</ref><blockquote><code>{5, 10, 15, 20}</code></blockquote><blockquote><code>{10, 13, 16, 19}</code></blockquote><blockquote><code>{2, 19, 26, 40}</code> </blockquote><blockquote><code>{18, 22, 23, 24}</code></blockquote>We start with the heads of each array and then build a binary tree from there.▼
▲
[[File:Binary Ideal Merge 1.png|centre|thumb]]
Line 54 ⟶ 91:
== External Sorting<ref>{{Cite book|title = Data Structures and Algorithm Analysis in C++, Third Edition|url = https://books.google.com/books?id=AijEAgAAQBAJ|publisher = Courier Corporation|date = 2012-07-26|isbn = 9780486172620|first = Clifford A.|last = Shaffer}}</ref> ==
K-way merges find
A multiway
== References ==
{{Reflist}}
▲* {{cite book| last = Knuth| first = Donald| authorlink = Donald Knuth| series = [[The Art of Computer Programming]]| volume= 3| title= Sorting and Searching| edition = 2nd| publisher = Addison-Wesley| year= 1998| chapter = Section 5.2.4: Sorting by Merging| pages = 158–168| isbn = 0-201-89685-0| ref = harv}}
▲* {{cite book|author1=Thomas H. Cormen|authorlink1=Thomas H. Cormen|author2=Charles E. Leiserson|author3=Ronald L. Rivest|authorlink3=Ron Rivest|author4=Clifford Stein|title=Introduction To Algorithms|url=https://books.google.com/books?id=NLngYyWFl_YC&pg=PA11|year=2001|publisher=MIT Press|isbn=978-0-262-03293-3|pages=28–29}}
[[Category:Sorting algorithms]]
|