Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by sleepingsquirrel (Hermit)
on Feb 03, 2005 at 21:12 UTC ( #427825=note: print w/ replies, xml ) Need Help??


in reply to Better mousetrap (getting top N values from list X)

Just as an aside, if you had lazy lists (coming in perl6) and the appropriate sort, the following should run in optimal O(X*log(N)) time, straight out of the box.

@topN = (sort @X)[0..($N-1)];


-- All code is 100% tested and functional unless otherwise noted.


Comment on Re: Better mousetrap (getting top N values from list X)
Select or Download Code
Re^2: Better mousetrap (getting top N values from list X)
by BrowserUk (Pope) on Feb 03, 2005 at 22:37 UTC

    How would lazy lists allow that?

    You would still have to sort the whole array before you can perform the slice, because the last value in the array could be the highest.


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


      -- All code is 100% tested and functional unless otherwise noted.

        Oh! A bubble sort. That's essentially what I did in my first attempt 427046, but it didn't fair very well. It would reasonable for very small N, but it very rapidly gets overtaken.

        And you'll notice, it still has to process the whole list on the first pass, so lazy lists don't really come into play.


        Examine what is said, not who speaks.
        Silence betokens consent.
        Love the truth but pardon error.
      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.

        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
Node Status?
node history
Node Type: note [id://427825]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-09-18 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (105 votes), past polls