Skip to content
Official PYQ · 2005

GATE CS 2005 Q39 — Searching, Sorting, Hashing

Explain with AI

Suppose there are ⌈log⁡n⌉\lceil \log n \rceil sorted lists of ⌊n/log⁡n⌋\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)

Choose one option
View worked solution

Answer: A

We are given k=⌈log⁡n⌉k = \lceil \log n \rceil sorted lists, where each list contains m=⌊n/log⁡n⌋m = \lfloor n / \log n \rfloor elements.

The total number of elements across all lists is:

N=k×m≈log⁡n×nlog⁡n=nN = k \times m \approx \log n \times \frac{n}{\log n} = n

To merge kk sorted lists into a single sorted list, we can use a min-heap of size kk:

  1. Build the Heap: Insert the first element of each of the kk lists into a min-heap.
    Time complexity: O(k)=O(log⁡n)O(k) = O(\log n).
  2. Extract and Insert: Repeat the following steps NN times (once for each element in the final sorted list):
    • Extract the minimum element from the heap and append it to the output list.
    • Insert the next element from the same list that contained the extracted element.
    Since the heap size is always at most kk, each extract-min and insert operation takes O(log⁡k)O(\log k) time.
    Total time for NN elements: O(Nlog⁡k)O(N \log k).

Substituting N=nN = n and k=log⁡nk = \log n into the complexity expression:

Total Time=O(k+Nlog⁡k)=O(log⁡n+nlog⁡log⁡n)=O(nlog⁡log⁡n)\text{Total Time} = O(k + N \log k) = O(\log n + n \log \log n) = O(n \log \log n)

Thus, the time complexity of producing the sorted list is O(nlog⁡log⁡n)O(n \log \log n), which matches Option A.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Searching, Sorting, Hashing questions