No, you don't need O(nk)
time - it can be done in O(n)
(average case).
The end result of the quickselect would be the k-th element at the k-th position from the end in the array, with smaller elements on the left, and larger elements on the right.
So all elements from that element to the right would be the k largest elements - it would be trivial to extract these in O(k)
(with a simple for-loop), which would end up with a total running time of O(n)
.
Alternatively, since you'd know the k-th element after running quickselect, you could just go through the array again and extract all elements larger or equal to that element (this could be done in a single pass - also O(n)
).
You'd need an additional O(k log k)
(to sort them) if you want to return them in sorted order.