[algogeeks] Re: How to store largest N values efficiently

2011-07-13 Thread John Reid
On 12/07/11 16:15, bittu wrote: John You Can Use MinHeap here is the algo 1) Build a Min Heap MH of the first k elements (arr[0] to arr[k-1]) of the given array. O(k) 2) For each element, after the kth element (arr[k] to arr[n-1]), compare it with root of MH. a) If the element is greater than t

[algogeeks] Re: How to store largest N values efficiently

2011-07-11 Thread John Reid
On 11/07/11 12:07, abhijith reddy wrote: Wouldn't a heap be ideal for this ? I thought it might. Do I just need to limit the heap to size 2 * N to avoid storing values I'll never need? Thanks, John. On Mon, Jul 11, 2011 at 3:35 PM, John Reid mailto:j.r...@mail.cryst.bbk.ac.

[algogeeks] How to store largest N values efficiently

2011-07-11 Thread John Reid
I have a procedure that generates N x M values sequentially. I want to store the N largest ones and discard the others. Obviously I can store all the values in a vector, sort it when it is full and then choose the top N values. Is there a more efficient way using a data structure that just stor