http://qs1969.pair.com?node_id=427825


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.