Question

When we have two sorted lists of numbers in non-descending order, and we need to merge...

When we have two sorted lists of numbers in non-descending order, and we need to merge them into one sorted list, we can simply compare the first two elements of the lists, extract the smaller one and attach it to the end of the new list, and repeat until one of the two original lists become empty, then we attach the remaining numbers to the end of the new list and it's done. This takes linear time. Now, try to give an algorithm using O(nlog k) time to merge k sorted lists (you can also assume that they contain numbers in non-descending order) into one sorted list, where n is the total number of elements in all the input lists. Use a binary heap for k-way merging

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Answer:

Firstly we have to construct the min-heap of the k-sublist and each sublist which is a node in the constructed min-heap.
We have several steps to follows:

Step-1. When we compare the two sublists, at the starting we can compare their first elements which is actually their minimum elements.
Step-2. The min-heap formation will cost be O(k) time.
Step-3. After the step 1 & step 2 we can run the minimum algorithm which can be extracted from the minimum element in the root list.
Step-4. Then Update the root list in the heap and after that heapify the min-heap as maintained by the new minimum element in the root list.
Step-5. If any root sub-list becomes empty in the step 4 then we can take any leaf sub-list from the root and heapify it.
Step-6. At every Extraction of the element it can take up to O(log k) time.

Hence, We can say that the extract of n element in the total whose
Running time will be O(n log k + k) which can be equal to the O(n log k+ k) (since k < n).

Add a comment
Know the answer?
Add Answer to:
When we have two sorted lists of numbers in non-descending order, and we need to merge...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 9. When we have two sorted lists of numbers in non-descending order, and we need to...

    9. When we have two sorted lists of numbers in non-descending order, and we need to merge them into one sorted list, we can simply compare the first two elements of the lists, extract the smaller one and attach it to the end of the new list, and repeat until one of the two original lists become empty, then we attach the remaining numbers to the end of the new list and it's done. This takes linear time. Now, try...

  • 1. Design an algorithm to find all the non-common elements in two sorted lists of numbers....

    1. Design an algorithm to find all the non-common elements in two sorted lists of numbers. What is the maximum number of comparisons your algorithm makes if the lengths of the two given lists are m and n, ?respectively 2. Estimate how many times faster it will be to find ged(98765, 56789) by Euclid's algorithm compared with the algorithm based on checking consecutive integers from min{m, n} down to gcd(m, n). 3. For each of the following functions, indicate how...

  • Optimal Merge Pattern - We saw from MergeSort that merging two sorted files together (one size...

    Optimal Merge Pattern - We saw from MergeSort that merging two sorted files together (one size m and one size n) took O(m+n) time. When merging multiple sorted files together, the merge can be accomplished by repeatedly merging sorted files in pairs. For example X1, X2, X3 are three sorted files of length 30, 20 and 10. There are multiple ways to merge them two-at-a-time (with the appropriate costs): (X1 merge X2) merge X3 X1 merge (X2 merge X3) (X1...

  • Discrete Mathematics Unsorted and Sorted Lists For linear search there was no requirement for the list...

    Discrete Mathematics Unsorted and Sorted Lists For linear search there was no requirement for the list to be organized in any manner. The linear search works for lists that are "unsorted." But what if the values in the list are given in ascending order? This would be a sorted list. With a sorted list, is there a more efficient way to find the target? Unsorted Lists (4 pts) Assume there is a sorting algorithm with order of growth O(n), where...

  • problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to...

    problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to write its pseudo-code). To sort an array A, you will then call Det-QuickSort(A, 1, n). You also need to provide the worst case time complexity analysis of your algorithm. 2. (20 points) Given a set of n distinct numbers, we wish to find the k largest in sorted order using a comparison-based algorithm. Give an algorithm that implements each of the following methods, and...

  • In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 pro...

    In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); //left recursion mergesort(a,mid+1,j); //right...

  • Suppose that we have two unsorted lists of integers A and B. The lists are the...

    Suppose that we have two unsorted lists of integers A and B. The lists are the same size, n. a) Write an algorithm that outputs how many integers occur in both lists. An integer occurs at most once in each given list. For example: if A = [1,2,5,7,13,19] and B = [2,9,13,14,19,22], then we can see that elements {2, 13, 19} occur in both lists, so the output will be 3. b) If the lists were sorted, say how we...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this...

    Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this question, however I found a lot of mistakes on it, where the guy declared a public class, which is not a thing in C language. Furthermore it is important to divide the question into two C files, one for part a and one for part b. hw2 Merge Sort algorithm using 2 processes a.) Define an integer array of 100...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT