edited by
20,229 views

2 Answers

Best answer
21 votes
21 votes
We can perform this in O(nlogK) time using heaps...

First, create a min-heap with first k+1 elements.Now, we are sure that the smallest element will be in this K+1 elements..Now,remove the smallest element from the min-heap(which is the root) and put it in the result array.Next,insert another element from the unsorted array into the mean-heap, now,the second smallest element will be in this..extract it from the mean-heap and continue this until no more elements are in the unsorted array.Next, use simple heap sort for the remaining elements.

Time Complexity---

O(k) to build the initial min-heap

O((n-k)logk) for remaining elements...

Thus we get O(nlogk)

Hence,B is the correct answer
selected by
0 votes
0 votes
Answer is B ,approach is heapify process
Take first k elements and make min. heap by build heap=o (k).
Now delete first min. element from the tree and insert next element from the array and again heapify ....and repeat same thing.
So total complexity
K+{log k +log k +log  k. ......(n-1) times }
=n(ogk)

Related questions

2 votes
2 votes
1 answer
4
yes asked Oct 6, 2015
1,385 views
for example array contain a[1 2 3 3 3 3 3 4 5] then retun(1)