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)
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)?
Pseudocode and algorithm of finding median of unordered array in linear time.
Pseudocode and algorithm of finding median of unordered array in linear time.
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)
(15 pts) Describe an O(n)-time algorithm that partitions a doubly linked list L into two doubly...
(15 pts) Describe an O(n)-time algorithm that partitions a doubly linked list L into two doubly linked lists L1 and L2, where L1 contains the odd-number-th elements in L and L2 contains the even-number-th elements in L. Elements in both L1 and L2 should appear in the same order as they appear in L. For example, if L contains the numbers 4, 7, 9, 1, and -3, in that order, then the output L1 should contain 4, 9, and -3,...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of comparable objects L, and a new item x, find the location (index) of x or indicated that x is not in L. 5a) Give a recursive algorithm for binary search. No, while, for, repeat etc statements are allowed. Only if, if else and assignment statements are allowed. 5b) Write a difference equation for the running time of your Binary Search algorithm. Solve your equation...
Recall our linear-time deterministic selection algorithm, SELECT. Give the pseudocode for a modified Quicksort algorithm, call...
Recall our linear-time deterministic selection algorithm, SELECT. Give the pseudocode for a modified Quicksort algorithm, call it AWESOME-QUICKSORT , that has worst-case run time O(nlogn). State the cost of your algorithm using a recurrence T(n), and solve then solve this recurrence any way you like (e.g., master method).
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array of size n into k subsets each of size n/k and recursively searches only one of these k subsets. Which one of these k subsets should be searched is decided by making (k − 1) comparisons, each of which can be done in constant time. Clearly, the recurrence relation for this k-ary search algorithm can be written as, T(n) = T(n/k) + (k −...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT