• retagged by
42,071 views
117 117 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)

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

19 Answers

Best answer
163 163 votes
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)$.

Correct Answer: $A$
• edited by
32 32 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)

10 10 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)
• edited by
7 7 votes

Please correct me if I am wrong.

  • See, if we use merge sort algorithm then it uses divide and conquer mechanism and divide the list into small parts and then merge them by using merge procedure.
  • Now in any level, there is n/logn elements in the array and we need to merge them. To merge two array of size n/2 merge procedure take O(n/2+n/2) = O(n) time.
  • So to merge two array of size n/logn it will take 2n/logn time.
  • Now total number of such pair will be number of array/2 =logn/2
  • so in each level total number of comparison/movement will be sum of comparison/movement of all the pairs.
  • Total number of pairs = logn/2
  • each pair take 2n/logn time to get merge.
  • So total time taken at each level will be (2n/logn)*logn/2 =n.
  • Now the depth of the tree formed by merge sort while dividing the problem into subproblem is equal to logm for m elements.
  • So for logn elements the depth of tree will be loglogn.
  • Now we merge each pair until it get merge till the root level of the tree so total loglogn level require and each level take time equals to n
  • So total time taken will be O(nloglogn)
Answer:
Position:
Show:

Related questions

197 197 votes
9 answers 9 answers
78.0k
78.0k views
Kathleen asked Sep 22, 2014
77,966 views
A $5$ stage pipelined CPU has the following sequence of stages:IF – instruction fetch from instruction memoryRD – Instruction decode and register readEX – Execute: ALU op...
32 32 votes
3 answers 3 answers
15.4k
15.4k views
gatecse asked Sep 21, 2014
15,372 views
Let $f(x)$ be the continuous probability density function of a random variable $x$, the probability that $a < x \leq b$, is :$f(b-a)$$f(b) - f(a)$$\int\limits_a^b f(x) dx...
32 32 votes
5 answers 5 answers
9.2k
9.2k views
go_editor asked Nov 15, 2016
9,164 views
We are given $9$ tasks $T_1, T_2, \dots, T_9$. The execution of each task requires one unit of time. We can execute one task at a time. Each task $T_i$ has a profit $P_i$...
59 59 votes
6 answers 6 answers
11.7k
11.7k views
go_editor asked Nov 14, 2016
11,736 views
Let $s$ and $t$ be two vertices in a undirected graph $G=(V,E)$ having distinct positive edge weights. Let $[X,Y]$ be a partition of $V$ such that $s \in X$ and $t \in Y$...