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

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.
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,...
(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 −...
In class, we learned that the running time of Karatsuba’s algorithm can be expressed as the...
In class, we learned that the running time of Karatsuba’s algorithm can be expressed as the recurrence T (n) = 3T (n/2) + n. We then used the substitution method to solve the recurrence. When using the substitution method, it seems difficult to make a guess about the upper bound on the running time. However, if we use the recursion tree method, it would be a lot easier. In this question, you are asked to solve this recurrence using the...
Write an algorithm to evaluate an infix expression in Java. Calculate its running time
Write an algorithm to evaluate an infix expression in Java. Calculate its running time
Write an algorithm for combining two skip lists in O(a + b) time, where a is...
Write an algorithm for combining two skip lists in O(a + b) time, where a is the number of keys in the first list, and b is the number of keys in the second list.
Problem 1: (10 marks) ALGORITHM COMPLEXITY:Write an algorithm (pseudocode but not a program) that takes a...
Problem 1: ALGORITHM COMPLEXITY:Write an algorithm (pseudocode but not a program) that takes a positive numberias an input, where i=a1a2...an,n>=3(numberwith 3 or more digits, every digit can have the value between 0 and 9–both inclusive). The algorithm should find the largest number jsuch that j=b1..bm, 1<=m<=n(numberwith m digits),andevery bk<=bk+1where1<=k<m and bk,bk+1ε{a1, a2, an}.Also, perform the computational complexity (time complexity)analysis of your algorithm and write the algorithm complexity in Big-Oh notation. Examples:Input: 23720, Output: 237Input: 9871, Output: 9Input: 1789, Output:1789Input: 2372891,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT