Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
K way merging advanced data structures materials
1.
2. Merging
• Merging is a process of combining two or more types of structures
into one single structure, which is an important component of
algorithms such as merge sort.
• If we have two or generally more arrays, we can merge them and get
a single array or list.
• Practically speaking, the size of the merged array is the same as the
total size of the merged arrays.
3. Sorted Merging
• There are various types of merging methods.
• Sorted merging simply combines the items of the structures that are
already in sorted order.
• Let’s see the following example. First, we take two or more sorted
arrays and compare the first items.
• The first elements of two sorted arrays are compared, and the smaller
element is taken and added to the output array:
4.
5. We continue comparing and appending smaller
items to the output array until the input arrays are
empty:
6. At this point, array1 is empty. We take the smaller item in array2 and append it to the
output since the other input array needs to be empty still. The process is repeated until
array2. :
7. End-to-End Merging
• This type of merging simply merges one structure at the end of the
other structure. Not to mention the structures that are merged can
either be in sorted or unsorted order.
• This technique simply appends one array to the end of another array,
which can be either sorted or unsorted:
8.
9. In our case, we appended array2 to array1 and created array3 with the same number of
elements as the merged arrays:
10. Two-Way Merge
• A two-way merging, also known as binary merging, is generally an
algorithm that takes two sorted lists and merges them into one list in
sorted order. It’s a widely used approach in merge sort that outputs
the minimum item in each step given list in sorted order and builds a
sorted list with all items in any of the input lists in proportion to the
total of the lengths of the input lists.
• If we are merging two arrays size i and j respectively, then the
merged array size will be the sum of i and j. Furthermore, merging
requires maximum (i + j) comparisons to get the merged array.
11. • The detailed implementation of the two-way merge is shown in the
diagrams below.
• We compare the first items in two arrays in sorted order and then
append the smaller element to the output array:
12. After appending the smaller item to our output
array, we continue comparing and appending
them to array3 until the input arrays are empty:
13. At this point, array1 is empty. The other input array must still be empty, so we take the smaller item in array2
and append it to array3. We repeat this process until array2 is empty.
By comparing the first items in each input array and appending them to our output array, we obtained a
merged sorted array with a size equal to the sum of the input array sizes.
14. Algorithm
The 2-way merging algorithm works by
comparing the first item of each list
and moving the smallest one to the top
of the output list.
In fact, this operation is repeated until
one list is empty, at which point all
entries of the second list are appended
to the final output list.
The space complexity is O(K) when the
complexity of time is O(1).
15. K way merge algorithms
• k-way merge algorithms, also
known as multiway merges, are
algorithms that take k-sorted lists
as input and produce one sorted list
as an output with size n equal to
the sum of sizes of all input arrays.
• The term “k-way merge algorithms”
refers to merging approaches that
accept more than two sorted lists:
• Pseudocode for the K-way merging
algorithm:
17. We first create tournament trees by comparing the list’s index 0 items and append
the winner with the least value to the output list:
18. We continue the process till the lists are empty:
In fact, the k-way merging technique performs well if k < 8. Otherwise, determining the smallest value in each step
would need a very large number of comparisons.
The space complexity is O(K), while the time complexity is O(N log K).