• edited by
10,526 views
33 33 votes

You have $n$ lists, each consisting of $m$ integers sorted in ascending order. Merging these lists into a single sorted list will take time:

  1. $O(nm \log  m)$
  2. $O(mn \log  n)$
  3. $O(m + n)$
  4. $O(mn)$

5 Answers

Best answer
40 40 votes

Answer is (B)

Since, $n$ lists of each size $m$.

Since, each list is sorted in ascending order use directly merge procedure of merge sort algo.

Take two list and merge..so one pair will take $\mathbf{2m}$ time.

So, total pairs in first level will be $n/2$. So total cost for one level is $\mathbf{(n/2)*2m=nm}$.

In next level cost for one pair is $4m$ and no of pairs will be $n/4$.. so next level cost will be $nm$.

So, like this each level will have cost $nm$.

No of levels will be when we have one complete list..

$\mathbf{n/2^k =1}$..

$\mathbf{k=\log_2^ \ n}$.

So, total cost  will be $\mathbf{ \log n *(nm)}$

• edited by
1 1 vote

Answer is B.

Apart from merge sort, this can also be done easily using a min Heap.

Given k sorted arrays of size n each, they can be merged into one single sorted array in time O(nk log k)

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

0 0 votes
By normal intuition also we can answer this

we have n lists

we have to merge into a single list

Time : mn(log(mn))

but here n>>m so log(mn) is simply logn also mn matters

hence answer is mnlog(n)
Answer:
Position:
Show:

Related questions

3 3 votes
1 1 answer
730
730 views
go_editor asked May 23, 2016
730 views
You are going abroad and you have to complete a number of formalities before you leave. Each task takes a full day to complete. Fortunately, you have an army of friends t...
7 7 votes
1 1 answer
1.3k
1.3k views
go_editor asked May 23, 2016
1,332 views
You are given two sorted lists of integers of size $m$ and $n$. Describe a divide and conquer algorithm for computing the $k$-th smallest element in the union of the two ...
6 6 votes
3 answers 3 answers
2.1k
2.1k views
go_editor asked May 23, 2016
2,115 views
The below question is based on following program:procedure mystery (A : array [1..100] of int) int i,j,position,tmp; begin for j := 1 to 100 do position := j; for i := j ...
9 9 votes
3 answers 3 answers
1.7k
1.7k views
go_editor asked May 23, 2016
1,747 views
The below question is based on the following program.procedure mystery (A : array [1..100] of int) int i,j,position,tmp; begin for j := 1 to 100 do position := j; for i :...