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

(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...
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[ ] ).
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.
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to...
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to 10 different arrays of integers. Each array has 100 integer elements. The arrays are filled with random integer numbers.
Design a program in JAVA that allows you to experiment with different sort algorithms. The algorithms...
Design a program in JAVA that allows you to experiment with different sort algorithms. The algorithms are shell sort and quick sort. Assume that input data is stored in a text file. Experimenting with a prototype data (integers from 1 to 10) to ensure that your implementation works correctly and the results match expectations. The program will sort what is in the text file and print the amount of comparisons and exchanges for both algorithms.
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would...
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would contain x randomly generated integers where the value of x would be 10000, 20000, 40000 and 80000 (inputs). The parts of the program should be followed as..... 1. Set x = 10000, randomly generate x integers. Call qsort function to sort these integers and get the execution time. 2. Randomly generate another x integers. Call your own sorting algorithm and get the execution time....
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After division, each process sorts its part of the array using an efficient algorithm. Then, the subarrays are merged into larger sorted subarrays. b. Analyze the communication and computation times if the number of processes is equal to the number of array elements, n. c. Repeat Part b if the number of processes is less than the number of array elements. Assume that the computation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT