Question

In: Computer Science

Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).

Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).

Solutions

Expert Solution

Assuming that we have function name Merge(A,B) is available, which combines two sorted lists A and B into single list C and return in time complexity O(|A|+|B|). Here we divide k sorted list into k/2 pairs of list and combine each pair of lists into single list in first step and then repeat the process for k/2 lists available. This process will continue till there is only one list available.

Assume each lists are stored in LIST_QUEUE such that LIST_QUEUE[ i ] will be ith sorted lists. For simplicity assume k = 2x for some integer x.

ALGORITHM(LIST_QUEUE , k)

1. While k > 1:-

2........For i = 1 to k :-

3.................A = LIST_QUEUE.Dequeue //dequeue the list form list queue

4.................B = LIST_QUEUE.Dequeue //dequeue the list form list queue

5.................C = Merge(A,B)

6.................LIST_QUEUE.Enqueue(C) //Enqueue sorted list C

7. If k == 1 then

8........return LIST_QUEUE.Dequeue //return the final single sorted list

Here since at each iterations, merging process over entire N elements is going on, which takes O(N) times. And this process will repeat (log k) times, hence total time complexity will be O(N log k).


Related Solutions

Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of...
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of your implementation is O(m)
a) Design in pseudocode an algorithm that gets as input an integer k, and a list...
a) Design in pseudocode an algorithm that gets as input an integer k, and a list of k integers N1, .., Nk, as well as a special value SUM. Your algorithm must locate a pair of values in the list that sum to the value SUM. For example, if your list of values is 3, 8, 13, 2, 17, 18, 10, and the value of SUM is 20, then your algorithm should output "either" the two values 2 and 18,...
The following is the pseudocode of algorithm that searches a sorted array of n items by...
The following is the pseudocode of algorithm that searches a sorted array of n items by dividing it into three sub arrays of almost n/3 items (7pt). Hint: refer Mergesort algorithm) Input: Sorted array of Keys(A) indexed from 1 to n. Output: Location, the Location of K in array A (hint: return 0 if K is no in A) index location3 (int A[1…n ], index L, index H) { index m1, m2; if (L > H) return 0; else   ...
Write the algorithm, following the ideas given in class, for merging two sorted arrays into one...
Write the algorithm, following the ideas given in class, for merging two sorted arrays into one sorted array. Note the algorithm is not the one for merge sort. 2) What is the best asymptotic upper bound for the algorithm? List reasoning steps.
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n)...
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n) algorithm to find the duplicates between two sorted arrays (m, n are the sizes of two arrays). For instance, for inputs {1,3,5,7} and {1,2,3,4}, the correct outputs should be {1,3
Suppose that we performed the algorithm SELECT whose running time is O(n) on 133 elements, and...
Suppose that we performed the algorithm SELECT whose running time is O(n) on 133 elements, and found the median of medians x by making groups of 5 elements. What is the maximum number of elements which are guaranteed to be greater than equals to x (without counting x, itself)?
Design an O(nlogn) algorithm that takes two arrays A and B (not necessarily sorted) of size...
Design an O(nlogn) algorithm that takes two arrays A and B (not necessarily sorted) of size n of real numbers and a value v. The algorithm returns i and j if there exist i and j such that A[i] + B[j] = v and no otherwise. Describe your algorithm in English and give its time analysis.
Pseudocode and algorithm of finding median of unordered array in linear time.
Pseudocode and algorithm of finding median of unordered array in linear time.
Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted)...
Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted) of size n of real numbers and a value v. The algorithm returns i and j if there exist i and j such that A[i] + B[j] = v and no otherwise. Describe your algorithm in English and give its time analysis.
What is the running time of Quicksort when the input array is sorted in ascending order?...
What is the running time of Quicksort when the input array is sorted in ascending order? (please explain in detail)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT