In computer science, K-Way Merge Algorithms or Multiway Merges are a specific type of Sequence Merge Algorithms that specialize in taking in multiple sorted lists and merging them into a single sorted list. These merge algorithms generally refer to merge algorithms that take in a number of sorted lists greater than two. 2-Way Merges are referred to as binary merges on the other hand and are also utilized in k-way merge algorithms. K-Way merge algorithms generally find use in sorting algorithms such as Patience Sorting.
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.[1]
Example[2]
Assume, that we have two arrays A[0..m-1] and B[0..n-1] that are sorted in ascending order and we want to merge them into an array C[0..m+n-1] with the same order.
- Introduce read-indices i, j to traverse arrays A and B, accordingly. Introduce write-index k to store position of the first free cell in the resulting array. Initially i = j = k = 0.
- At each step: if both indices are in range (i < m and j < n), choose minimum of (A[i], B[j]) and write it to C[k]. Otherwise go to step 4.
- Increase k and index of the array, algorithm located minimal value at, by one. Repeat step 2.
- Copy the rest values from the array, which index is still in range, to the resulting array.
Applying 2-Way Merge when k > 2
If the number of sorted lists k is greater than 2, the 2-Way Merge function found in merge sort can still be used to merge everything into a single sorted list.
Non-optimal
Let D={n1, ... , nk} be the set of sequences to be merged. Pick ni, nj∈ D and then merge them together using the merge function. The new set D is then D' = (D - {ni, nj}) ∪ {ni+nj}. This process is repeated until |D| = 1. The question then becomes how to pick ni and nj. How the merge algorithm picks ni and nj determines the cost of the overall algorithm. The worst case running time for this algorithm reaches O(k2 • n) where n is the number of elements in the merged list.
optimal merge pattern
The optimal merge pattern is found by utilizing a Greedy algorithm that selects the two shortest lists at each time to merge. This technique is similar to the one used in Huffman coding. The algorithm picks ni, nj∈ D such that |n| ≥ |ni| and |n| ≥ |nj| ∀ n∈ D.[3]
On the optimal merge pattern on the hand can reduce the running time to O(k • n • log k).[4] By choosing the shortest lists to merge each time, this lets the algorithm minimize how many times it is necessary to copy the same value into each new merged list.
Ideal Merge
The Ideal Merge technique is another merge method for merging greater than two lists except it does not use the 2-Way merge technique. The ideal merging technique was discussed and saw use as a part of UnShuffle Sort.[5]
Given a group of sorted lists S that we want to merge into list S', the algorithm is as follows:
- Each list is sorted by the value of its head element
- Then the head element of the first list is removed and placed into S'
- After the head element of the first list is removed, it is resorted according to the value of its new head element
- This repeats until all lists are empty
The head elements are generally stored in a priority queue. Depending on how the priority queue is implemented, the running time can vary. If ideal merge keeps its information in a sorted list, then inserting a new head element to the list would be done through a Linear search and the running time will be Θ(k • n) where n is the total number of elements in the sorted lists, and k is the total number of sorted lists.
On the other hand, if the sorted items in a heap, then the running time becomes Θ(n log k).
Binary Tree Implementation
As an example, the above technique can be implemented using a binary tree for its priority queue.[6] This can help limit the number of comparisons between the element heads. The binary tree is built containing the results of comparing the head of each array. The topmost node of the binary tree is then popped off and its leaf is refilled with the next element in its array.
As an example, let's say we have 4 sorted arrays:[7]
{5, 10, 15, 20}
{10, 13, 16, 19}
{2, 19, 26, 40}
{18, 22, 23, 24}
We start with the heads of each array and then build a binary tree from there.
The nodes from each array are compared to each other, before the value 2 is found as the lowest list head element. That value is then popped off, and its leaf is refilled with the next value in the list.
The value 2 is repopulated by the next value in the sorted list, 19. The comparisons end with 5 being the smallest value, and thus the next value to be popped off. This continues until all of the sorted lists are empty.
External Sorting[8]
K-way merges find greater use in external sorting procedures. External sorting algorithms are a class of sorting algorithms that can handle massive amounts of data. External sorting is required when the data being sorted do not fit into the main memory of a computing device (usually RAM) and instead they must reside in the slower external memory (usually a hard drive). K-way merge algorithms usually take place in the second stage of external sorting algorithms, much like they do for merge sort.
A multiway merge like that discussed in ideal 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 together 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.
References
- ^ F.K.Hwang and S. Lin, \A Simple Algorithm for Merging Two Disjoint Linearly Ordered Sets", SIAM Journal on Computing 1 (1972), 31-39.
- ^ "ALGORITHM TO MERGE SORTED ARRAYS (Java, C++) | Algorithms and Data Structures". www.algolist.net. Retrieved 2015-11-19.
- ^ KABAT, MANAS RANJAN (2013-08-21). DESIGN AND ANALYSIS OF ALGORITHMS. PHI Learning Pvt. Ltd. ISBN 9788120348066.
- ^ Discrete Mathematics. Addison-Wesley. 1997-01-01. ISBN 9780673980397.
- ^ Art S. Kagel, Unshuffle Algorithm, Not Quite a Sort?, Computer Language Magazine, 3(11), November 1985.
- ^ Horowitz, Ellis; Sahni, Sartaj (1983-01-01). Fundamentals of data structures. Computer Science Press. ISBN 9780914894209.
- ^ "kway - Very fast merge sort - Google Project Hosting". code.google.com. Retrieved 2015-11-22.
- ^ Shaffer, Clifford A. (2012-07-26). Data Structures and Algorithm Analysis in C++, Third Edition. Courier Corporation. ISBN 9780486172620.
Further reading
- Knuth, Donald (1998). "Section 5.2.4: Sorting by Merging". Sorting and Searching. The Art of Computer Programming. Vol. 3 (2nd ed.). Addison-Wesley. pp. 158–168. ISBN 0-201-89685-0.
{{cite book}}
: Invalid|ref=harv
(help) - Thomas H. Cormen; Charles E. Leiserson; Ronald L. Rivest; Clifford Stein (2001). Introduction To Algorithms. MIT Press. pp. 28–29. ISBN 978-0-262-03293-3.