Arjun Suresh (talk | contribs) |
Arjun Suresh (talk | contribs) |
||
Line 18: | Line 18: | ||
[[Category:Algorithms & Data Structures Questions from GATE]] | [[Category:Algorithms & Data Structures Questions from GATE]] | ||
[[Category:GATE2005]] | [[Category:GATE2005]] | ||
− |
Suppose there are $\log n$ sorted lists of $\frac{n}{\log n}$ elements each. The time complexity of producing a sorted list of all these elements is: (Hint:Use a heap data structure)
(A) $O(n\log \log n)$
(B) $ \Theta(n\log n)$
(C) $\Omega(n\log n)$
(D) $(n^{\frac{3}{2}})$
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 came in 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 is $O(1)$ and inserting an element on a heap of size $n$ is $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)$.
Suppose there are $\log n$ sorted lists of $\frac{n}{\log n}$ elements each. The time complexity of producing a sorted list of all these elements is: (Hint:Use a heap data structure)
(A) $O(n\log \log n)$
(B) $ \Theta(n\log n)$
(C) $\Omega(n\log n)$
(D) $(n^{\frac{3}{2}})$
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 came in 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 is $O(1)$ and inserting an element on a heap of size $n$ is $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)$.