628 views
9 votes
9 votes

Given an array of $n$ elements, you have to design an algorithm to find the $k$ smallest elements in sorted order. The time complexity of the best such algorithm assuming comparison based sorting will be

  1. $O(k \log n)$
  2. $\Theta(n + k \log k)$
  3. $\Theta(n^2)$
  4. $\Theta (n \log k)$

2 Answers

Best answer
13 votes
13 votes
The best possible algorithm will find the $k^{th}$ smallest element in $O(n)$ time and then move all elements smaller than this to its left - partition part of quick sort which again takes $O(n).$ Finally, these $k$ elements can be sorted in $O(k \log k)$ time.
selected by
2 votes
2 votes

we have to find k smallest elements in sorted order,for that 1st we’ve to find Kth smallest element.

kth smallest element – https://gateoverflow.in/8243/gate2015-2-45 //    Time complexity = O(N)

Now we’ve to output the k smallest lists in sorted order,so sort those k numbers using klogk and output the list.

So, overall time complexity = O(n + klogk)

Answer:

Related questions

3 votes
3 votes
1 answer
1