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

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): More...

#include "ADS/Contract.h"
#include "ADS/sort/Sorted.h"
#include <algorithm>
#include <bit>
#include <cstddef>
#include <functional>
#include <utility>
#include <vector>
#include "ADS/sort/impl/SortingNetworks.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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: 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):

  • mergeExchangeNetwork() is the merge exchange of Batcher (Algorithm M of Knuth) for any \( n \), \( O( n \log^2 n ) \) comparators in \( O( \log^2 n ) \) rounds;
  • bitonicNetwork() is the bitonic sorter of Batcher for \( n \) a power of 2: two halves sorted in opposite directions form a bitonic sequence, which a half-cleaner splits into two bitonic halves, every item of the first not larger than every item of the second; also \( O( n \log^2 n ) \) comparators;
  • applyNetwork() runs a network on a vector, and the 0-1 principle (a network sorts every input if it sorts every input of 0 and 1) is what the tests check.

The contracts [see Contract.h]: the comparators of a network join two distinct places within \( n \), a bitonic network needs \( n \) a power of 2, and applyNetwork() a vector of at least \( n \) items. The functions are defined in SortingNetworks.tpp.

See also
K. E. Batcher, Sorting networks and their applications, Proceedings of the AFIPS Spring Joint Computer Conference 32, 307-314, 1968, doi:10.1145/1468075.1468121
D. E. Knuth, The Art of Computer Programming, vol. 3, 2nd ed., Addison-Wesley, 1998, sections 5.2.2 (Algorithm M) and 5.3.4
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa