I have a question about finding the kth largest element using a min-heap. The algorithm is as follows:
We take the first k elements and build a minheap
Let Sk be the smallest element in S.
Look at a new element, from the remaining n-k elements.
If the new element is larger than Sk, then it replaces it in S, and reorder the heap.
S then will have a new smallest element.
After looking at all the other elements Sk is the answer
I do not understand this algorithm. For example, let the numbers be 1, 2, 3, 4. We want to find 4th largest, which is 4. But when we use the algorithm, we take first 4 elements, build the minheap, and Sk is 1.
What am I doing wrong? I would appreciate if someone could help. Thanks