Content deleted Content added
m Pzoxicuvybtnrm moved page K-way merge algorithms to K-way merge algorithm: should be singular |
fix capitalisation |
||
Line 1:
{{DISPLAYTITLE:''k''-way merge algorithm}}
{{orphan|date=November 2015}}
In [[computer science]], '''
== 2-
A 2-
Denote by A[1..p] and B[1..q] two arrays sorted in increasing order.
Further, denote by C[1..n] the output array.
The canonical 2-Way
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.
Line 14 ⟶ 16:
In this case the algorithm copies the remaining elements of B or A into C and terminates.
==
The
Denote by n the total number of elements.
n is equal to the size of the output array and the sum of the sizes of the k input arrays.
Line 23 ⟶ 25:
Several algorithms that achieve this running time exist.
=== Iterative 2-Way
The problem can be merged by iteratively merging two of the k arrays using a 2-
The running time can be improved by iteratively merging the first with the second, the third with the fourth, and so on. 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).
Line 31 ⟶ 33:
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 strategy 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 strategy explained in the previous paragraph needs Θ(n log k) running time, while the improved one only needs Θ(n) running time.
=== Direct
The basic idea of a direct
A straightforward implementation would scan all k arrays to determine the minimum.
This straightforward implementation results in a running time of Θ(kn).
Line 67 ⟶ 69:
===== Example =====
[[File:Binary Ideal Merge 1.png|centre|thumb]]
Line 79 ⟶ 81:
=== Lower Running Time Bound ===
One can show that no comparison-based
The proof is a straightforward reduction from comparison-based sorting.
Suppose that such an algorithm existed, then we could construct a comparison-based sorting algorithm with running time O(n f(n)) as follows:
Chop the input array into n arrays of size 1.
Merge these n arrays with the
The resulting array is sorted and the algorithm has a running time in O(n f(n)).
This is a contradiction to the well-known result that no comparison-based sorting algorithm with a worst case running time below O(n log n) exists.
== External sorting ==
{{See also|External sorting}}
A multiway merge allows for the files outside of memory to be merged in fewer passes than in a binary merge. If there are 6 runs that need be merged then a binary merge would need to take 3 merge passes, as opposed to a 6-way merges single merge pass. This reduction of merge passes is especially important considering the large amount of information that is usually being sorted in the first place, allowing for greater speed-ups while also reducing the amount of accesses to slower memory.
|