TAOCP 5.4.1 Exercise 17
Let the input keys satisfy $K_1 > K_2 > \cdots > K_N.$ Algorithm R initializes a selection tree with the first $P$ records.
Section 5.4.1: Multiway Merging and Replacement Selection
Exercise 17. [20] [20] What is output by Algorithm R when the input keys are in decreasing order, Kk, > Ky>---> Kw?
Verified: yes
Solve time: 1m34s
Solution
Let the input keys satisfy
$K_1 > K_2 > \cdots > K_N.$
Algorithm R initializes a selection tree with the first $P$ records. These $P$ keys all have run number $1$. During run generation, at each step the algorithm outputs the smallest key currently eligible for the active run and replaces it with the next input key, assigning run number $S+1$ if the replacement key is smaller than the last output key of the current run.
Structure of the first run
The initial $P$ active keys are exactly
$K_1, K_2, \ldots, K_P,$
and they are all available for run $1$.
Among these, the smallest is $K_P$, so the first output is $K_P$.
The next input key is $K_{P+1}$, and since
$K_{P+1} < K_P,$
it cannot belong to the current run. Algorithm R therefore assigns it to run $2$. The active set for run $1$ still contains all remaining keys from ${K_1,\ldots,K_P}$ except $K_P$.
At any intermediate stage of run $1$, the active keys belonging to run $1$ are exactly the subset of ${K_1,\ldots,K_P}$ not yet output. The replacement keys introduced from the input always belong to run $2$, since every new input element is smaller than the last output from run $1$ once the process begins.
Therefore, run $1$ never mixes run-$2$ elements into its selection process. The algorithm continues selecting among the remaining run-$1$ keys until none remain. The order of extraction is
$K_P, K_{P-1}, \ldots, K_1,$
since each step selects the smallest remaining element among ${K_1,\ldots,K_P}$.
Thus the first run outputs exactly the $P$ keys
$K_P, K_{P-1}, \ldots, K_1.$
Formation of subsequent runs
After the first run is completed, exactly $P$ keys have been read from input and exactly $P$ keys have been output. The next run begins with the $P$ keys that were introduced as replacements during the first run:
$K_{P+1}, K_{P+2}, \ldots, K_{2P}.$
These keys are precisely those assigned run number $2$ during the first run construction. Since the input sequence is strictly decreasing, the same argument applies verbatim inside this new run. The smallest available key is $K_{2P}$, and successive outputs are
$K_{2P}, K_{2P-1}, \ldots, K_{P+1}.$
Hence run $2$ also consists of exactly $P$ outputs in reverse order of its input block.
Inductive repetition
Repeating the same reasoning shows that for each block
$K_{(i-1)P+1}, K_{(i-1)P+2}, \ldots, K_{iP},$
Algorithm R produces a run consisting of exactly these $P$ keys in the order
$K_{iP}, K_{iP-1}, \ldots, K_{(i-1)P+1}.$
Each run contains exactly $P$ elements, except possibly the final run if $P \nmid N$, which contains the remaining $N \bmod P$ elements in the same reversed order.
Final output description
The complete output is the concatenation of runs
$$ (K_P, K_{P-1}, \ldots, K_1),; (K_{2P}, K_{2P-1}, \ldots, K_{P+1}),; \ldots $$
with the last run truncated if necessary.
Each run is strictly increasing, since within every block
$K_{iP} < K_{iP-1} < \cdots < K_{(i-1)P+1}$
holds under the assumption $K_1 > K_2 > \cdots > K_N$.
Thus Algorithm R transforms a globally decreasing sequence into runs of length $P$ (except possibly the last), each run being the reverse of a consecutive input block.
$\boxed{\text{Runs of size } P \text{ (last possibly shorter), each run equal to the reversed order of its input block}}$
This completes the solution. ∎