Question

In: Computer Science

Design and analysis of Algorithms: How do I write a k way merge sort algorithm from...

Design and analysis of Algorithms: How do I write a k way merge sort algorithm from a merge sort algorithm that splits 2 arrays while sorting?

Solutions

Expert Solution

Merge Sort and K way Merge Sort

Hey Student, get ready lets us grease up our mind for the Data Structure lesson on Merge sort and K way merge sort. We will be learning merge sort and k way merge sort and how we can analysis and design to solve write a k way merge sort algorithm from a merge sort algorithm that splits 2 arrays while sorting

Merge Sort

Here, arr[] is the input array, l is left index and r is right index

MergeSort(arr[], l, r)

If r > l

     1. Find the middle point to divide the array into two halves:

             middle m = (l+r)/2

    2. Call mergeSort for first half:  

             Call mergeSort(arr, l, m)

     3. Call mergeSort for second half:

             Call mergeSort(arr, m+1, r)

     4. Merge the two halves sorted in step 2 and 3:

             Call merge(arr, l, m, r)

Time complexity of Merge Sort is O(nLogn)  in all 3 cases (worst, average and best)

We can use the above algorithm to sort the array in K way Merge Sort. In K way merge sort k is the no of arrays that we want to merge that are sorted, we can use merge sort to sort the arrays if not already sorted.

K way Merge Sort

An efficient solution is to use heap data structure. The time complexity of heap based solution is O(N Log k).

1. Create an output array.
2. Create a min heap of size k and insert 1st element in all the arrays into the heap
3. Repeat following steps while priority queue is not empty.
     a) Remove minimum element from heap (minimum is always at root) and store it in output array.
     b) Insert next element from the array from which the element is extracted. If the array doesn’t have any more elements, then do nothing.

Example: k=3

arr[][] = { {1, 3},

                  {4, 2 , 6},

                  {10, 0, 9, 11}} ;

In the above k arrays are not sorted, use merge sort algorithm to sort them, below are the sorted array after merge sort.

arr[][] = { {1, 3},

                  {2, 4, 6},

                  {0, 9, 10, 11}} ;

Now we will put k way merge sort using min heap, taking all the first element of the array and put them in the heap and checking for the min value and put them in the output array. Below is the output after k way merge sort.

Min heap: 1 , 2 , 0

Output array: [0]

Min heap: 1 , 2, 9

Output array: [0, 1]

Min heap : 3 , 2 , 9

Output array: [0, 1, 2]

Min heap : 3 , 2 , 9

Output array: [0, 1, 2]

.

.

.

Similarly on each step, we take the min value from the heap and put in the output array to get the final k way sorted array.

Output: 0 1 2 3 4 6 9 10 11


Related Solutions

implement merge sort,quick sort, and radix sort algorithms in java and test how long it will...
implement merge sort,quick sort, and radix sort algorithms in java and test how long it will take to sort with random data sets of users input numbers.
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
The multi-merge sort algorithm (M&M sort) works like merge sort, except that instead of dividing the...
The multi-merge sort algorithm (M&M sort) works like merge sort, except that instead of dividing the array into 2 partitions, it divides the array into p partitions. It recursively sorts each of the p partitions and then merges the results doing a p-way merge (except, of course, for the base case of a singleton or empty array). (a) Write the recurrence for T(n) for M&M sort for the case where p is a constant. Then, give a closed-form solution to...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
Consider a sorting algorithm that combines merge sort and insertion sort algorithm. We still use divide...
Consider a sorting algorithm that combines merge sort and insertion sort algorithm. We still use divide and conquer like merge sort, however when the number of elements in an array is at most k elements (k is a parameter), we stop dividing the elements as the regular merge sort, instead, we call the insertion sort. Assuming we have defined the following two procedures: insertion-sort(A[p..q]) which sort the subarray A[p..q] merge(A[p,q,r]) which merges the sorted subarray A[p..r] and A[r+1..q] Try to...
Which sorting algorithms is most efficient to sort string consisting of ASCII characters? Heap sort, Merge...
Which sorting algorithms is most efficient to sort string consisting of ASCII characters? Heap sort, Merge sort, insertion sort, bubble sort, selection sort or quick sort?
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++...
Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++ programming language; #include<iostream>
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT