Tu banner alternativo

Pairwise sorting network

In today's article we are going to talk about Pairwise sorting network, a topic that has generated a lot of discussion in recent times. It doesn't matter if you are an expert in the field or just starting to explore this field, this article will provide you with key information and interesting facts that will help you better understand the topic. From its origins to its relevance today, through its possible practical applications, we are going to delve into Pairwise sorting network in a detailed and exhaustive manner, so that at the end of reading you feel more informed and with a broader perspective on this exciting topic. Join us on this journey of discovery!

Tu banner alternativo
Pairwise sorting network
Visualization of the Pairwise sorting network with 16 inputs
Visualization of the Pairwise sorting network with 16 inputs
ClassSorting algorithm
Data structureArray
Worst-case performance parallel time
Worst-case space complexity space and sequential time
OptimalNo

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

  1. Sort consecutive pairwise bits of the input (corresponds to the first layer of the diagram)
  2. Sort all pairs into lexicographic order by recursively sorting all odd bits and even bits separately (corresponds to the next three layers of 2+4+8 columns of the diagram)
  3. Sort the pairs in nondecreasing order using a specialized network (corresponds to the final layers of the diagram)

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]

Pseudocode

 n ← length of array
 k ← smallest power of two, k ≥ n
 
 for k/2 ≥ p ≥ 1, p in k/2, k/4, k/8, … 4, 2, 1 do
   
   (these comparisons can all be done in parallel)
   for 0 ≤ a < n, a in 0, p*2, p*4, p*6, p*8, p*10, … do
     for 0 ≤ b < p, b in 0, 1, 2, … p-3, p-2, p-1 do
       i ← a + b
       j ← a + b + p
       if j < n then compare and swap elements i and j end if
   
   for k/2 ≥ q ≥ p*2, q in k/2, k/4, k/8, … p*8, p*4, p*2 do
     
     (these comparisons can all be done in parallel)
     for 0 ≤ c < n, c in 0, p*2, p*4, p*6, p*8, p*10, … do
       for 0 ≤ d < p, d in 0, 1, 2, … p-3, p-2, p-1 do
         i ← c + d + p
         j ← c + d + q
         if j < n then compare and swap elements i and j end if
   
   repeat q
 repeat p

References

  1. ^ 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
  2. ^ "Sorting Networks".