25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 85<br />

2.4.4 Polyphase Sort<br />

We have now discussed the necessary techniques <strong>and</strong> have acquired the proper background to<br />

investigate <strong>and</strong> program yet another sorting algorithm whose performance is superior to the balanced sort.<br />

We have seen that balanced merging eliminates the pure copying operations necessary when the<br />

distribution <strong>and</strong> the merging operations are united into a single phase. The question arises whether or not<br />

the given sequences could be processed even more efficiently. This is indeed the case; the key to this next<br />

improvement lies in ab<strong>and</strong>oning the rigid notion of strict passes, i.e., to use the sequences in a more<br />

sophisticated way than by always having N sources <strong>and</strong> as many destinations <strong>and</strong> exchanging sources <strong>and</strong><br />

destinations at the end of each distinct pass. Instead, the notion of a pass becomes diffuse. The method<br />

was invented by R.L. Gilstad [2-3] <strong>and</strong> called Polyphase Sort.<br />

It is first illustrated by an example using three sequences. At any time, items are merged from two<br />

sources into a third sequence variable. Whenever one of the source sequences is exhausted, it immediately<br />

becomes the destination of the merge operations of data from the non-exhausted source <strong>and</strong> the previous<br />

destination sequence.<br />

As we know that n runs on each input are transformed into n runs on the output, we need to list only the<br />

number of runs present on each sequence (instead of specifying actual keys). In Fig. 2.14 we assume that<br />

initially the two input sequences f0 <strong>and</strong> f1 contain 13 <strong>and</strong> 8 runs, respectively. Thus, in the first pass 8 runs<br />

are merged from f0 <strong>and</strong> f1 to f2, in the second pass the remaining 5 runs are merged from f2 <strong>and</strong> f0 to f1,<br />

etc. In the end, f0 is the sorted sequence.<br />

f0 f1 f2<br />

13 8<br />

5 0 8<br />

0 5 3<br />

3 2 0<br />

1 0 2<br />

0 1 1<br />

1 0 0<br />

Fig. 2.14. Polyphase mergesort of 21 runs with 3 sequences<br />

A second example shows the Polyphase method with 6 sequences. Let there initially be 16 runs on f0,<br />

15 on f1, 14 on f2, 12 on f3, <strong>and</strong> 8 on f4. In the first partial pass, 8 runs are merged onto f5. In the end,<br />

f1 contains the sorted set of items (see Fig. 2.15).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!