Roedy Green wrote:
>
> On Mon, 11 Aug 2003 17:50:45 -0400, Eric Sosman <>
> wrote or quoted :
>
> >replacement selection
>
> Is this what is called a "tournament" sort? How does it work?
*Very* briefly, since the topicality seems shaky: Read as
many items as you possibly can. Then select the least item and
write it to the merge file. Fill the vacated spot by reading
another input item, which gives two cases: if the new item is
greater than or equal to the last item output, just keep on
repeating the select-write-replace loop. If the new item is
less than the last item output, it can't be added to the run
currently being written, so it must be deferred to the next
run. Set it aside for now, thus reducing the number of items
still available for selection. If any items still are available
for selection, continue the select-write-replace loop.
Eventually, memory becomes completely full of deferred items,
none of which can belong to the current output run. Finish off
that run (writing trailer records, closing files, whatever) and
begin a new one, using all those deferred items as the initial
contents of the selection tree.
This process is something like an M-way merge of the input
data with itself, where M is the number of items that fit in
memory. For further details, including an analysis of why the
expected run length is approximately 2*M, see Knuth "The Art
of Computer Programming, Volume III: Sorting and Searching,"
Section 5.4.1.
--