Possible Duplicate:
Worst-case O(n) algorithm for doing k-selection
Given the following question :
In an integer array with N elements , find the minimum k elements (k << N)
You can assume that N is a large number.
I’m thinking about a minimum heap , anyone has a better solution ?
Regards
If K << N, min heap is good enough because creation of heap is O(n), and if K << N selecting first K items is at most O(N), otherwise you could use selection algorithm to find Kth smallest element in
O(n)then select numbers which are smaller than found item. (Sure if some numbers are equal to Kth element select till fillKitems).