Rolling your own can be educational. I did it because I thought that it would be trivial to do. By the time I finished..well I was educated. ;-)

Note that I used the quicksort rather than heapsort because I assumed that the issue is memory usage, and quicksort is easier to do.

#! /usr/bin/perl use strict; # Demo usage here my $sorter = Sort::Limited->new( comp => sub { $_[0] cmp $_[1] }, limit => 20, ); for (1..100) { $sorter->add($_); } print join "|", $sorter->extract(); # The silly module - not well tested, etc. package Sort::Limited; use Data::Dumper; use Carp; sub add { my $self = shift; ELEM: foreach my $elem (@_) { my $node = $self->{data}; my $pos = 0; while (exists $node->{elem}) { # Traverse one level more if ($self->{comp}->($elem, $node->{elem}) < 0) { $node->{cnt}++; $node = $node->{left}; } else { $pos += 1 + $node->{cnt}; if ($self->{limit} < $pos) { # Filter data - we are past our limit $node->{right} = { cnt=>0 }; next ELEM; } $node = $node->{right}; } } # Fill in this node $node->{elem} = $elem; $node->{left} = { cnt=>0 }; $node->{right} = { cnt=>0 }; # And check whether to replace the root node... if ($self->{limit} < $self->{data}{cnt}) { $self->{data} = $self->{data}{left}; } } } sub extract { my $self = shift; my @results = _extract($self->{data}); my $last = @results < $self->{limit} ? @results : $self->{limit} - 1 +; return @results[0..$last]; } sub _extract { my $node = shift; if (exists $node->{elem}) { return ( _extract($node->{left}), $node->{elem}, _extract($node->{right}), ); } else { return (); } } sub new { my ($class, %args) = @_; my $self = {}; $self->{comp} = delete $args{comp} || sub {$_[0] cmp $_[1]}; $self->{limit} = delete $args{limit} || 10; if (%args) { $Data::Dumper::Indent = 1; confess ("Unprocessed arguments: ", Dumper(\%args)); } $self->{data} = { cnt => 0, }; return bless $self, $class; }

In reply to Re: Heap sorting in perl by Anonymous Monk
in thread Heap sorting in perl by blakem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.