ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches

The sort of a List by relinking its cells, beyond the book, stable, the engineered counterpart of naturalMergeSort(), which copies the items through the positions as the book does; no item is copied or moved, and the positions stay on their items. More...

Files

file  ADS/sequence/ListSort.h
 The sort of a List [see List.h] by relinking its cells, beyond the book, stable, the engineered counterpart of naturalMergeSort() [see SequenceAlgorithms.h], which copies the items through the positions as the book does; no item is copied or moved, and the positions stay on their items.

Functions

template<class T, bool P, class C = std::less<>>
void ADS::listSort (List< T, P > &L, C less=C())
 the list \( L \) sorted by relinking its cells, stable

Detailed description

The sort of a List by relinking its cells, beyond the book, stable, the engineered counterpart of naturalMergeSort(), which copies the items through the positions as the book does; no item is copied or moved, and the positions stay on their items.

The definitions, the invariants and the costs of the area are in Theory of sequences and lists.

Function documentation

◆ listSort()

template<class T, bool P, class C = std::less<>>
void ADS::listSort ( List< T, P > & L,
C less = C() )

the list \( L \) sorted by relinking its cells, stable

In pseudocode (the natural merge of Knuth, section 5.2.4, by runs and by a binary counter as McIlroy, beyond the book), \( R[ 0 .. 63 ] \) the runs merged so far, \( R[ i ] \) made of about \( 2^i \) runs of the input and the higher places holding the earlier runs:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{listSort}( L ) \cr 2 & \quad \textbf{if}\ \text{the items are numbers in their natural } \text{order}\ \textbf{then} \cr 3 & \qquad A \gets \text{the pairs } ( \mathrm{key}( x ) , x ) \text{ of } \text{the cells } x \text{ of } L;\ \text{sort } A \text{ by radix} \cr 4 & \qquad \text{relink the cells in the order of } A;\ \textbf{return} \cr 5 & \quad R[ 0 .. 63 ] \gets \textbf{nil} \cr 6 & \quad \textbf{while}\ \text{cells are left}\ \textbf{do} \cr 7 & \qquad r \gets \text{the next run, not decreasing, or strictly } \text{decreasing and reversed} \cr 8 & \qquad i \gets 0 \cr 9 & \qquad \textbf{while}\ R[ i ] \ne \textbf{nil}\ \textbf{do}\ r \gets \mathrm{merge}( R[ i ] , r );\ R[ i ] \gets \textbf{nil};\ i \gets i + 1 \cr 10 & \qquad R[ i ] \gets r \cr 11 & \quad r \gets \textbf{nil} \cr 12 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ 63\ \textbf{do} \ \textbf{if}\ R[ i ] \ne \textbf{nil}\ \textbf{then}\ r \gets \mathrm{merge}( R[ i ] , r ) \cr 13 & \quad \text{relink the cells in the order of } r \text{, setting } \text{the predecessors} \end{array}} \]

where \( \mathrm{merge}( a , b ) \) follows the successors only and takes the cell of \( a \) on equal items, which keeps the sort stable.

Postcondition
\( L \) is sorted by less, with the same cells (an audit)