http://www.geeksforgeeks.org/merge-k-sorted-arrays/

The Gateway to Computer Science Excellence

First time here? Checkout the FAQ!

x

+26 votes

Suppose there are $\lceil \log n \rceil$ sorted lists of $\lfloor n /\log n \rfloor$ elements each. The time complexity of producing a sorted list of all these elements is: (Hint:Use a heap data structure)

- $O(n \log \log n)$
- $\Theta(n \log n)$
- $\Omega(n \log n)$
- $\Omega\left(n^{3/2}\right)$

+3

Refer this for better understanding of question.

http://www.geeksforgeeks.org/merge-k-sorted-arrays/

http://www.geeksforgeeks.org/merge-k-sorted-arrays/

+42 votes

Best answer

Since we have $\log n$ lists we can make a min-heap of $\log n$ elements by taking the first element from each of the $\log n$ sorted lists. Now, we start deleting the min-element from the heap and put the next element from the sorted list from which that element was added to the heap. (This identity can be done by making a structure of two values, one for the number and one for identifying the origin sorted list of that number and storing this structure in the heap). In this way each delete and the corresponding insert will take $O(\log\log n)$ time as delete in heap of size $n$ is $O(\log n)$ and inserting an element on a heap of size $n$ is also $O(\log n)$. (here, heap size is $\log n$). Now, we have a total of $\log n \times \frac{n}{\log n} = n$ elements. So, total time will be $O(n \log\log n)$.

0

How the deletion in heap takes O(1) time? Can you please explain? I think it should be O(log n) (if no. of elements are n) in this case it will be O(log log n) although the answer will still remain same.

0

Sorry Arjun Sir, but I am bit confused in it.

Yes, Saurabh is right that to preserve heap property, taking out min element will take O(logN) time ( so here it is loglogn ). But how this deletion is actually working here in this question??

1. We have created min-heap with O(log n) by considering the first element in each sorted list

2. In order to take out min element (deleting) will be down by swapping min with last element... So, here are we swapping the entire two nodes or just 2 elements???? It cannot be elements, because then sorted order cannot be maintained in each node AND how can it even be entire node swap? because after swap, in order to delete min element ( which is start of the list) we have to take it to the last through n/log(n) elements (or shift rest of the list to left by 1 position) and the go for preserving the heap property. So, answer will change in this case. (Kindly note that, if we ignore this fact on shifting.. Then I am getting the correct answer as nloglog(n) )

Can you clarify where am I going wrong??

+1

Sir if we have 2 sorted lists containing m and n elements then we can merge it using merge procedure from merge sort in O(m+n) so here if we have k sorted lists containing e elements then we can merge it in O(ke) which would be O(logn * (n/logn)) = O(n). So why not use this ?

+8 votes

We can merge x arrays of each size y in in O(xy*Logy) time using Min Heap.

x = n/Logn

y = Logn

We get O(n/Logn * Logn * Log Log n) which is O(nLogLogn)

+6 votes

Number of List = Log(n) , Number of elements in each list = n/Log(n)

Take the pair appraoch( like merge in pairs , (1,2) is first pair , (3,4) is the second pair , (4,5) , (6,7)...... (log(n) - 1,logn) will be the log(n)/2 th pair and merge using merge procedure of mergesort which takes Theta(k) where k is the number of elements in big one list.

Thus for first attempt the work will be [ ( n/log(n) ) * ( log(n)/2 ) ]= n/2 as theta(n/log(n)) work has to be done for each pair and total log(n)/2 pairs are there

Second attempt , as now we have ( log(n)/2 ) list so again make pairs but now element in each list is 2n/log(n)

thus work will be => ( 2n/logn) * (log(n)/4 ) = n/2

similliarly again number of list will be half then make pairs thus for third attempt work will

be = (4n/logn) * (log(n)/8) = n/2

and so on....

total log(logn) attempt will be there , after that we will get single list of n elements

Thus (n/2 + n/2 + n/2 + ....... log(logn) terms )

thus ans = (n/2)* ( log(logn) ) = n log(logn)

Take the pair appraoch( like merge in pairs , (1,2) is first pair , (3,4) is the second pair , (4,5) , (6,7)...... (log(n) - 1,logn) will be the log(n)/2 th pair and merge using merge procedure of mergesort which takes Theta(k) where k is the number of elements in big one list.

Thus for first attempt the work will be [ ( n/log(n) ) * ( log(n)/2 ) ]= n/2 as theta(n/log(n)) work has to be done for each pair and total log(n)/2 pairs are there

Second attempt , as now we have ( log(n)/2 ) list so again make pairs but now element in each list is 2n/log(n)

thus work will be => ( 2n/logn) * (log(n)/4 ) = n/2

similliarly again number of list will be half then make pairs thus for third attempt work will

be = (4n/logn) * (log(n)/8) = n/2

and so on....

total log(logn) attempt will be there , after that we will get single list of n elements

Thus (n/2 + n/2 + n/2 + ....... log(logn) terms )

thus ans = (n/2)* ( log(logn) ) = n log(logn)

+1 vote

Lets start with 2 list each of size n

to merge them we will need, O( n+n ) = O (2n)

similarly to merge k list of size n = O ( kn )

Now to merge log(n) lists of size n/log(n)

it will take O ( log(n) * n / log(n) ) = O ( n )

to merge them we will need, O( n+n ) = O (2n)

similarly to merge k list of size n = O ( kn )

Now to merge log(n) lists of size n/log(n)

it will take O ( log(n) * n / log(n) ) = O ( n )

0

Merging 2 lists of size n will take O(n) , that's fine, but the question is on two - way merge (sorry i did not mention it before) so we have to merge lists multiple times rather than just merging lists once.

If merge was to be done only once it would be O(n).

This is my understanding of the question, please correct me if I am wrong.

If merge was to be done only once it would be O(n).

This is my understanding of the question, please correct me if I am wrong.

0

ya you are also making a good point here,

for first merge its = O ( 2n )

second = O ( 3n )

ans so on for (k-1)th merge = O ( kn )

---------------------------------------------

in total = O ( 2n + 3n + ......+ kn ) = O ( k^{2} n )

so in case of this question = O ( log^{2}(n) * n / log(n) ) = O ( n * log(n) )

- All categories
- General Aptitude 1.2k
- Engineering Mathematics 4.9k
- Digital Logic 2k
- Programming & DS 3.6k
- Algorithms 3k
- Theory of Computation 3.9k
- Compiler Design 1.5k
- Databases 2.9k
- CO & Architecture 2.5k
- Computer Networks 2.9k
- Non GATE 949
- Others 1.3k
- Admissions 411
- Exam Queries 419
- Tier 1 Placement Questions 17
- Job Queries 55
- Projects 9

34,786 questions

41,762 answers

118,950 comments

41,409 users