P is for Practical | |
PerlMonks |
Re: Re: Heap sorting in perlby blakem (Monsignor) |
on Apr 05, 2003 at 19:58 UTC ( [id://248332]=note: print w/replies, xml ) | Need Help?? |
A heap is a specialized datastructure that can be thought of as an "automatically sorted array" given a somewhat scaled down definition of "sorted". Sorted in this case means that the largest element is always easy to find and remove, and inserting new elements is also easy.
Given that, our steps to find the smallest M elements would be:
As you can see, this is very similar to the strategy you proposed. In fact, you could view heaps as a datastructure designed specifically to implement this algorithm efficiently. -Blake
In Section
Seekers of Perl Wisdom
|
|