in reply to A heap of medians: efficiency vs. speed
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:
Note that ntop returns the n top elements unsorted and has O(N) complexity.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";
ntopsort returns them sorted, but it has O(NlogN) complexity.
update: BTW, the n in ntop doesn't stand for nth but for numeric!
|
---|