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

Rearrangements of a vector in place, by swaps or with a small buffer. More...

Files

file  ADS/sequence/Rearrangements.h
 Rearrangements of a vector in place, by swaps or with a small buffer: the partition into three classes, the alternating order, the sort of a vector whose items are near their places, and the cost of sorting by swaps.

Functions

template<class T, class F>
std::pair< std::size_t, std::size_t > ADS::dutchNationalFlag (std::vector< T > &A, F &&color, std::size_t *swaps=nullptr)
 the items of \( A \) by color( x ), 0 then 1 then 2, with \( n \) reads of the colors and at most \( n \) swaps, counted in *swaps if it is given; \( ( i , j ) \) with \( A[ 0 .. i ) \) of color 0, \( A[ i .. j ) \) of color 1 and \( A[ j .. n ) \) of color 2
template<class T, class C = std::less<>>
void ADS::wiggleSort (std::vector< T > &A, C less=C())
 \( A[ 0 ] \le A[ 1 ] \ge A[ 2 ] \le \cdots \), by swaps of neighbors; \( n - 1 \) comparisons
template<class T, class C = std::less<>>
void ADS::sortKSorted (std::vector< T > &A, std::size_t k, C less=C())
 \( A \) sorted, each item at most \( k \) places away from its place in the sorted order, by a heap of \( k + 1 \) items; \( O( n \log k ) \)
template<class T, class W = T>
requires std::is_arithmetic_v< T >
std::pair< std::size_t, W > ADS::minimumSwapSort (const std::vector< T > &A)
 the fewest swaps that sort \( A \), and the least total cost of the swaps that sort it, a swap costing the sum of its two items, computed in the type W (the least when no item is negative; with two items of negative sum no least cost exists); \( O( n \log n ) \)

Detailed description

Rearrangements of a vector in place, by swaps or with a small buffer.

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

Function documentation

◆ dutchNationalFlag()

template<class T, class F>
std::pair< std::size_t, std::size_t > ADS::dutchNationalFlag ( std::vector< T > & A,
F && color,
std::size_t * swaps = nullptr )

the items of \( A \) by color( x ), 0 then 1 then 2, with \( n \) reads of the colors and at most \( n \) swaps, counted in *swaps if it is given; \( ( i , j ) \) with \( A[ 0 .. i ) \) of color 0, \( A[ i .. j ) \) of color 1 and \( A[ j .. n ) \) of color 2

In pseudocode (Dijkstra, chapter 14, beyond the book), keeping the invariant (4) of the theory of the area:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{dutchNationalFlag}( A ) \cr 2 & \quad i \gets 0;\ j \gets 0;\ k \gets n \cr 3 & \quad \textbf{while}\ j < k\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \mathrm{color}( A[ j ] ) = 0\ \textbf{then} \ \text{swap } A[ i ] \text{ and } A[ j ];\ i \gets i + 1;\ j \gets j + 1 \cr 5 & \qquad \textbf{else if}\ \mathrm{color}( A[ j ] ) = 1 \ \textbf{then}\ j \gets j + 1 \cr 6 & \qquad \textbf{else}\ k \gets k - 1;\ \text{swap } A[ j ] \text{ and } A[ k ] \cr 7 & \quad \textbf{return}\ ( i , j ) \end{array}} \]

Precondition
color( x ) is 0, 1 or 2
Postcondition
the colors in order (an audit)

◆ minimumSwapSort()

template<class T, class W = T>
requires std::is_arithmetic_v< T >
std::pair< std::size_t, W > ADS::minimumSwapSort ( const std::vector< T > & A)

the fewest swaps that sort \( A \), and the least total cost of the swaps that sort it, a swap costing the sum of its two items, computed in the type W (the least when no item is negative; with two items of negative sum no least cost exists); \( O( n \log n ) \)

In pseudocode (Knuth, section 1.3.3, on the cycles of a permutation, beyond the book), each cycle of \( k > 1 \) items needing \( k - 1 \) swaps at the cost (5) of the theory of the area:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{minimumSwapSort}( A ) \cr 2 & \quad t[ i ] \gets \text{the rank of } A[ i ] \text{ in the sorted } \text{order};\ M \gets \min_i A[ i ] \cr 3 & \quad \mathit{swaps} \gets 0;\ \mathit{cost} \gets 0 \cr 4 & \quad \textbf{foreach}\ i\ \textbf{with}\ i \text{ not seen} \ \textbf{do} \cr 5 & \qquad \text{follow } i , t[ i ] , t[ t[ i ] ] , \ldots \text{ to } i \text{, marking them seen:}\ k \text{ items, of sum } s \text{ and least } m \cr 6 & \qquad \textbf{if}\ k > 1\ \textbf{then} \cr 7 & \qquad\quad \mathit{swaps} \gets \mathit{swaps} + k - 1 \cr 8 & \qquad\quad \mathit{cost} \gets \mathit{cost} + \min( s + ( k - 2 ) m , s + m + ( k + 1 ) M ) \cr 9 & \quad \textbf{return}\ ( \mathit{swaps} , \mathit{cost} ) \end{array}} \]

Precondition
the items of \( A \) are distinct (an audit)

◆ sortKSorted()

template<class T, class C = std::less<>>
void ADS::sortKSorted ( std::vector< T > & A,
std::size_t k,
C less = C() )

\( A \) sorted, each item at most \( k \) places away from its place in the sorted order, by a heap of \( k + 1 \) items; \( O( n \log k ) \)

In pseudocode (an exercise of the collections of the book), \( H \) a heap of the least item on the window \( A[ i .. i + k ] \) of the items not yet placed:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{sortKSorted}( A , k ) \cr 2 & \quad w \gets \min( k , n - 1 ) + 1\quad \triangleright\ A[ i ] \text{ is among the first } w \text{ items not yet placed} \cr 3 & \quad H \gets \text{a min-heap of } A[ 0 .. w - 1 ] \cr 4 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 5 & \qquad \textbf{if}\ i + w < n\ \textbf{then}\ A[ i ] \gets \text{the minimum of } H \text{, replaced by } A[ i + w ] \text{ (one sift)} \cr 6 & \qquad \textbf{else}\ A[ i ] \gets \mathrm{deleteMin}( H ) \end{array}} \]

Precondition
\( A \) is \( k \)-sorted (an audit)
Postcondition
\( A \) sorted, a permutation of its items (an audit)

◆ wiggleSort()

template<class T, class C = std::less<>>
void ADS::wiggleSort ( std::vector< T > & A,
C less = C() )

\( A[ 0 ] \le A[ 1 ] \ge A[ 2 ] \le \cdots \), by swaps of neighbors; \( n - 1 \) comparisons

In pseudocode (an exercise of the collections of the book), one pass of swaps of neighbors, each keeping the order of the pair before it:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{wiggleSort}( A )\quad \triangleright\ A[ 0 ] \le A[ 1 ] \ge A[ 2 ] \le \cdots \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 2\ \textbf{do} \cr 3 & \qquad \textbf{if}\ A[ i ] , A[ i + 1 ] \text{ are out of the order that the parity of } i \text{ asks} \cr & \qquad\quad \text{(}A[ i ] \le A[ i + 1 ] \text{ for } i \text{ even, } \ge \text{ for } i \text{ odd)}\ \textbf{then}\ A[ i ] \leftrightarrow A[ i + 1 ] \end{array}} \]

Postcondition
\( A \) alternates, and holds its items (an audit)