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

Sorting networks, beyond the book. More...

Files

file  ADS/sort/SortingNetworks.h
 Sorting networks, beyond the book: sequences of comparators \( ( i , j ) \), \( i \ne j \), each putting the smaller of \( A[ i ] \) and \( A[ j ] \) in \( A[ i ] \), fixed in advance whatever the input, so that the comparators of a round touch disjoint places and run in parallel (in hardware, on SIMD lanes, on a GPU):

Typedefs

using ADS::Comparator = std::pair< std::size_t , std::size_t >
 a comparator \( ( i , j ) \) of a network: the smaller of \( A[ i ] \) and \( A[ j ] \) goes to \( A[ i ] \)

Functions

std::vector< Comparator > ADS::mergeExchangeNetwork (std::size_t n)
 the merge exchange of Batcher for \( n \) items
std::vector< Comparator > ADS::bitonicNetwork (std::size_t n)
 the bitonic sorter for \( n \) items
template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::applyNetwork (Store &&A, const std::vector< Comparator > &N, C less=C())
 the comparators of \( N \) applied to \( A \) in their order
template<class T, class C = std::less<>>
void ADS::applyNetwork (std::vector< T > &A, const std::vector< Comparator > &N, C less=C())
 applyNetwork() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { applyNetwork( v , std::forward< P >( p )... ); }
decltype(auto) ADS::applyNetwork (R &&A, P &&... p)
 applyNetwork() on the range \( A \) [see SortRange], the other arguments as for a vector, in place: on the span of its items if \( A \) is contiguous, on its view otherwise, since the sort reads the items by their ranks only

Detailed description

Sorting networks, beyond the book.

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

Typedef documentation

◆ Comparator

using ADS::Comparator = std::pair< std::size_t , std::size_t >

a comparator \( ( i , j ) \) of a network: the smaller of \( A[ i ] \) and \( A[ j ] \) goes to \( A[ i ] \)

Function documentation

◆ applyNetwork() [1/3]

template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { applyNetwork( v , std::forward< P >( p )... ); }
decltype(auto) ADS::applyNetwork ( R && A,
P &&... p )

applyNetwork() on the range \( A \) [see SortRange], the other arguments as for a vector, in place: on the span of its items if \( A \) is contiguous, on its view otherwise, since the sort reads the items by their ranks only

◆ applyNetwork() [2/3]

template<class T, class C = std::less<>>
void ADS::applyNetwork ( std::vector< T > & A,
const std::vector< Comparator > & N,
C less = C() )

applyNetwork() above on a vector

◆ applyNetwork() [3/3]

template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::applyNetwork ( Store && A,
const std::vector< Comparator > & N,
C less = C() )

the comparators of \( N \) applied to \( A \) in their order

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{applyNetwork}( A , N ) \cr 2 & \quad \textbf{foreach}\ ( i , j ) \in N \text{ in order}\ \textbf{do} \cr 3 & \qquad \textbf{if}\ A[ j ] < A[ i ]\ \textbf{then}\ A[ i ] \leftrightarrow A[ j ] \end{array}} \]

Precondition
each comparator is within \( A \)

◆ bitonicNetwork()

std::vector< Comparator > ADS::bitonicNetwork ( std::size_t n)
inline

the bitonic sorter for \( n \) items

In pseudocode (Batcher, beyond the book), \( \oplus \) the exclusive or of the bits and \( \& \) their and, the runs of \( k \) items made sorted in alternate directions by the half-cleaners of distances \( k / 2 , k / 4 , \ldots , 1 \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{bitonicNetwork}( n ) \cr 2 & \quad N \gets \text{an empty sequence} \cr 3 & \quad \textbf{for}\ k \gets 2 , 4 , \ldots , n\ \textbf{do} \cr 4 & \qquad \textbf{for}\ j \gets k / 2 , k / 4 , \ldots , 1\ \textbf{do} \cr 5 & \qquad\quad \textbf{foreach}\ i < n \text{ with } l = i \oplus j > i \ \textbf{do} \cr 6 & \qquad\qquad \textbf{if}\ i \mathbin{\&} k = 0\ \textbf{then} \ \text{append } ( i , l )\ \textbf{else}\ \text{append } ( l , i ) \cr 7 & \quad \textbf{return}\ N \end{array}} \]

Precondition
\( n \) is a power of 2, or 0
Postcondition
each comparator joins two distinct places below \( n \)

◆ mergeExchangeNetwork()

std::vector< Comparator > ADS::mergeExchangeNetwork ( std::size_t n)
inline

the merge exchange of Batcher for \( n \) items

In pseudocode (Algorithm M of Knuth, section 5.2.2, beyond the book), \( t = \lceil \log_2 n \rceil \) and \( \& \) the and of the bits:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{mergeExchangeNetwork}( n ) \cr 2 & \quad N \gets \text{an empty sequence};\ \textbf{if}\ n < 2 \ \textbf{then return}\ N \cr 3 & \quad \textbf{for}\ p \gets 2^{t-1} , 2^{t-2} , \ldots , 1\ \textbf{do} \cr 4 & \qquad q \gets 2^{t-1};\ r \gets 0;\ d \gets p \cr 5 & \qquad \textbf{loop} \cr 6 & \qquad\quad \textbf{foreach}\ i \text{ with } i + d < n \text{ and } i \mathbin{\&} p = r\ \textbf{do}\ \text{append } ( i , i + d ) \text{ } \text{to } N \cr 7 & \qquad\quad \textbf{if}\ q = p\ \textbf{then}\ \textbf{break} \cr 8 & \qquad\quad d \gets q - p;\ q \gets q / 2;\ r \gets p \cr 9 & \quad \textbf{return}\ N \end{array}} \]

Postcondition
each comparator \( ( i , j ) \) has \( i < j < n \)