Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Better mousetrap (getting top N values from list X)

by Anonymous Monk
on Feb 04, 2005 at 08:22 UTC ( #427983=note: print w/replies, xml ) Need Help??


in reply to Re^2: Better mousetrap (getting top N values from list X)
in thread Better mousetrap (getting top N values from list X)

I don't understand. If you were to extract the largest element of the list, you'd make one pass and find it, even if the largest element is at the end. No need to sort the entire array here.

The same with finding the topN. Instead of keeping track of the largest element so far, you keep track of the largest N elements so far. If you do it in a heap, you can add an element, or remove the smallest element in O(log N) time. Still need one pass through the array. Don't have to sort the array. Note that if N equals the size of the list, you perform a sort in O(N log N) time. Which is optimal.

  • Comment on Re^3: Better mousetrap (getting top N values from list X)

Replies are listed 'Best First'.
Re^4: Better mousetrap (getting top N values from list X)
by BrowserUk (Patriarch) on Feb 04, 2005 at 08:47 UTC

    If you read the rest of the thread, you'll see that I (a) know that; (b) implemented that.

    In this subthread, the discussion (started by another monk) centers around using a lazy sort/lazy lists in P6 to produce the subset--which I was countering.

    But read the whole (sub)thread if your really interested.


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://427983]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2022-09-25 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (116 votes). Check out past polls.

    Notices?