Increasing Run Lengths Using Replacement Selection
Replacement Selection Procedure:
Read a collection of records and sort them using heapsort. The resulting heap is called the primary heap.
Instead of writing the entire primary heap in sorted order, write only the record whose key has the lowest value.
Bring in a new record and compare the values of its key with that of the key that has just been output.
- If the new key value is higher, insert the new record into its proper place in the primary heap along with the other records that are being selected for output.
- If the new record’s key value is lower, place the record in a secondary heap of records with key values smaller than those already written.
Repeat Step 3 as long as there are records left in the primary heap and there are records to be read. When the primary heap is empty, make the secondary heap into the primary heap and repeat steps 2 and 3.