Pairwise sorting network
![]() Visualization of the Pairwise sorting network with 16 inputs | |
Class | Sorting algorithm |
---|---|
Data structure | Array |
Worst-case performance | parallel time |
Worst-case space complexity | non-parallel time |
The pairwise sorting network is a sorting network discovered and published by Ian Parberry in 1992 in Parallel Processing Letters.[1] The pairwise sorting network has the same size (number of comparators) and depth as the odd–even mergesort network. At the time of publication, the network was one of several known networks with a depth of . It requires comparators and has depth .
The sorting procedure implemented by the network is as follows (guided by the zero-one principle):
- Sort consecutive pairwise bits of the input (corresponds to the first layer of the diagram)
- Sort all pairs into lexicographic order by recursively sorting all odd bits and even bits separately (corresponds to the next 14 layers of the diagram)
- Sort the pairs in nondecreasing order using a specialized network (corresponds to the final layers of the diagram)
Pseudocode[]
Given the number n of elements to sort, this is one possible non-recursive technique to calculate the index values of the elements to sort:
// Given a positive integer n, being the number of elements to sort
// Note: subscripts are numbered from 0 to (n - 1)
a = 1
while (a < n)
{
b = a; c = 0;
while (b < n)
{
// Compare and sort elements with indices (b - a) and (b)
b++; c++;
if (c >= a)
{
c = 0; b += a;
}
}
a *= 2
}
a /= 4 // Note: all integer divisions hereon automatically truncate the fractional part
e = 1
while (a > 0)
{
d = e
while (d > 0)
{
b = (d + 1) * a; c = 0;
while (b < n)
{
// Compare and sort elements with indices (b - d*a) and (b)
c++; b++;
if (c >= a)
{
c = 0; b += a;
}
}
d /= 2
}
a /= 2
e = 2*e + 1;
}
Relation to Batcher odd-even mergesort[]
The pairwise sorting network is very similar to the Batcher odd-even mergesort, but differs in the structure of operations. While Batcher repeatedly divides, sorts and merges increasingly longer subsequences, the pairwise method does all the subdivision first, then does all the merging at the end in the reverse sequence. In certain applications like encoding cardinality constraints, the pairwise sorting network is superior to the Batcher network.[2]
References[]
- ^ Parberry, Ian (1992), "The Pairwise Sorting Network" (PDF), Parallel Processing Letters, 2 (2, 3): 205–211, doi:10.1142/S0129626492000337, S2CID 2986739, archived from the original (PDF) on 2019-10-29
- ^ "Sorting Networks".
External links[]
- Sorting Networks – Archive of web page by the author.
- Sorting algorithms
- Algorithms and data structures stubs