in reply to A heap of medians: efficiency vs. speed

There are better algorithms to find the mediam (or, in general, any element at position nth). The one you are proposing is still O(NlogN) and it can be done in O(N). See the Selection Algorithm entry in the Wikipedia.

In Perl, you can use my module Sort::Key::Top to select the top n elements from a list. For instance, to get the median from a list of numbers:

use List::Util qw(shuffle max); use Sort::Key::Top qw(ntop); my @data = shuffle (0..100); my $mediam = ntop -1, ntop 1 + @data / 2, @data; # O(N) # or # $mediam = max ntop 1 + @data / 2, @data; # O(N) # $mediam = (ntopsort 1 + @data / 2, @data)[-1]; # O(NlogN)! print "mediam: $mediam\n";
Note that ntop returns the n top elements unsorted and has O(N) complexity.

ntopsort returns them sorted, but it has O(NlogN) complexity.

update: BTW, the n in ntop doesn't stand for nth but for numeric!