I am looking for efficient way to return top k
elements from an input array.
One way would be to sort the array and return the k
elements from end of array.
There are other methods suggested here, one of which uses the quickselect algorithm, but from my understanding, quickselect returns only the k
-th element in an unsorted array. After it returns, elements to left and right of k
are still unsorted.
So it should be something like this:
while k>0{
quickselect(int[] arr, k);
k--;
}
Quickselect is O(n)
and we do that for k
times, so the overall time complexity is O(n*k)
.
But the data in the post suggest that this is better than O(n log n)
.
Selecting the top 200 from a million sample would mean 200 million
in the former case but 20 million
in the latter. Clearly this is far better.
Is my understanding how quickselect can be employed to select the top 200 elements correct?