edited by
663 views
4 votes
4 votes

Let $L$ be a linked list of $n$ elements and $I$ be another linked list of $k$ node positions in $L$ ($n/4 < k < n/2)$ and elements in $I$ are not necessarily in sorted order. The best possible algorithm (use $O(1)$ extra space if required and assuming only comparison based sorting) to print the values in $L$ as per the node positions in $I$ will run in _____

  1. $\Theta(n)$
  2. $\Theta (n \log n)$
  3. $\Theta (n .k \log k) $
  4. $\Theta (nk)$
edited by

1 Answer

Best answer
11 votes
11 votes
The best algorithm here will do sorting of the linked list $I$ which can be done in $\Theta(k \log k)$ time and then one can simply do a traversal of $L$ in $\Theta(n).$ So, total complexity is $\Theta(k \log k) + \Theta(n).$

Since, $n/4 < k < n/2,$ we get $ k = \Theta(n).$

So, $\Theta(k \log k) + \Theta(n) \implies \Theta(n \log n) + \Theta(n) = \Theta(n \log n).$
selected by
Answer:

Related questions

3 votes
3 votes
1 answer
4
gatecse asked Aug 9, 2020
227 views
The cut vertices in the given graph areE and FA, C and DC, E and FB and C