Suppose there are sorted lists of elements each. The time complexity of producing a sorted list of all these elements is: (Hint:Use a heap data structure)
View worked solution
Answer: A
We are given sorted lists, where each list contains elements.
The total number of elements across all lists is:
To merge sorted lists into a single sorted list, we can use a min-heap of size :
- Build the Heap: Insert the first element of each of the lists into a min-heap.
Time complexity: . - Extract and Insert: Repeat the following steps 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.
Total time for elements: .
Substituting and into the complexity expression:
Thus, the time complexity of producing the sorted list is , which matches Option A.
Free practice · Your answers on this page are not saved.
