Sorting Networks
Uri ZwickTel Aviv University
May 2015
Comparators
๐ฅ๐ฆ
min (๐ฅ , ๐ฆ )max (๐ฅ , ๐ฆ)
Can we use comparators to build efficient sorting networks?
๐ฅ๐ฆ
min (๐ฅ , ๐ฆ )max (๐ฅ , ๐ฆ)
Comparator networks
A comparator network is a network composed of comparators.
There are input wires, each feeding a single comparator.
Each output of a comparator is either an output wire, or feeds a single comparator.
The network must be acyclic.
Number of output wires must also be .
โStandard formโ
Exercise: Show that any comparator networkis equivalent to a network in standard form.
A simple sorting network
5 comparators3 levels
Insertion sort
๐๐๐๐ก (๐)
Selection/bubble sort
๐๐๐๐ก (๐)
Selection/bubble Sort
Size = Depth =
Exercise: Any sorting network that only compares adjacent lines must be of size at least
Exercise: Prove that the network on the next slide, whose depth is , is a sorting network
Odd-Even Transposition Sort
Size = Depth =
The 0-1 principle
Theorem: If a network sort all 0-1 inputs,
then it sort all inputs
The 0-1 principle
Lemma: Let be a monotone non-decreasing function. Then,
if a network maps to ,then it maps to
Proof: By induction on the number of comparisons using
The 0-1 principle
Suppose that a network is not a sorting network.
Proof:
It then maps some to , where , for some .
Let , iff , 0 otherwise.
The network maps to
Thus, the network does not sort all 0-1 inputs.
Sorting by merging
Batcherโs odd-even merge
๐
๐
Batcherโs odd-even merge
๐
๐
Batcherโs odd-even merge
To merge with :
Split into and
Split into and
Merge odd-indexed items to create
Merge even-indexed items to create
Compare/swap
Does it really work?
Batcherโs odd-even merge
๐ (1 ,1)
๐ (2 ,2)
Batcherโs odd-even merge -
Batcherโs odd-even merge -
Odd-even merge Odd-even sort
Batcherโs odd-even merge
Suppose that starts with 0โs and that starts with 0โs.
Then starts with 0โs,and starts 0โs.
The difference is either 0,1 or 2!
Proof using the 0-1 principle.
There is a problem only if difference is 2and last level of comparators fixes it.
Batcherโs odd-even merge
0 0 0 0 0 1 1 10 0 0 0 0 1 1 1
๐๐
0 0 0 0 1 1 1 10 0 0 0 0 1 1 1
๐๐
0 0 0 1 1 1 1 10 0 0 0 0 1 1 1
๐๐
Batcherโs odd-even merge
Exercise: Justify the use of the 0-1 principle.
Exercise: Prove the correctness of the odd-even merging network directly without relying on the 0-1 principle.
Batcherโs odd-even mergeSize โ number of comparators
๐ (๐ ,0 )=๐ (0 ,๐ )=0
๐ (๐ ,๐)=2๐ (๐2 ,๐2 )+(๐โ1)
๐ ( 2๐ ,2๐ )=๐2๐+1
๐ (๐ ,๐)=๐ lg๐+ยฟ๐ (๐)ยฟ
๐ (1 ,1 )=1
๐ (๐ ,๐ )=๐ (โ ๐2 โ , โ ๐2โ )+๐ (โ ๐2 โ , โ ๐
2โ )+โ ๐+๐โ1
2โ
No better merging networks are known for any !Are the odd-even merge networks optimal?
Bitonic sequences
A sequence is bitonic iff it is acyclic shift of a strict bitonic sequence
8 6 4 1 2 5 7 9
4 1 2 5 7 9 8 6
A sequence is strict bitonic iff it is aconcatenation of a decreasing sequence
and an increasing sequence
1 4 2 3
A Bitonic sorter
A (strict) bitonic sorter is a network that sorts every (strict) bitonic sequence.
I and are sorted,then is bitonic.
Thus, a strict bitonic sorter can serve as a merging network
Batcherโs bitonic sorter
๐
Is this different from odd-even merge?
A strict binary bitonic sequence โ
The odd and even subsequences are alsostrict binary bitonic sequences.
Simple proof using the 0-1 principle.
The difference between the number of 0โs in the two sorted sequences is one of .
Batcherโs bitonic sorter
By induction they are sorted correctly.
Final level of comparators fixes the problem.
Exercise: Show that the difference between the number of 0โs in the odd
and even subsequences of is
Batcherโs bitonic sorter
Note: We do not need this exact formulain the proof given in the previous slide.
Seeing that the difference is is immediate.
Very regular structure!
When , there are levels with
comparators each.
Lines and are compared at level iff
they differ only in the -th most significant bit
Batcherโs bitonic sorter for n=
Batcherโs bitonic sorter for n=
Alternative recursive definition for
Sorts general bitonic sequences
The AKS sorting networks[Ajtai-Komlรณs-Szemerรฉdi (1983)]
There are sorting networks of depth,and hence size.
The construction is fairly complicated.
The constant factors are very large.