b = Merge_Sort(a,n)
if n < 100
return seqSort(a, n);
b1 = Merge_Sort(a[0,…,n/2-1], n/2);
b2 = Merge_Sort(a[n/2,…,n-1], n/2);
return Merge (b1, b2);
par_merge
\((B_i, A_i)\)perform many comparisons in parallel
comparators and network topology
a bitonic sequence is a sequence of elements \((a_0,a_1,\ldots,a_{n-1})\) with the property that either (1) there exists an index \(i, 0\leq i\leq n-1\), such that \((a_0,\ldots,a_i)\) is monotonically increasing and \((a_{i+1},\ldots,a_{n-1})\) is monotonically decreasing, or (2) there exists a cyclic shift of indices so that (1) is satisfied.
Let \(s=(a_0,\ldots,a_{n-1})\) be a bitonic sequence such that \[a_0\leq a_1 \leq \cdots \leq a_{n/2-1}\] and \[a_{n/2}\geq a_{n/2+1} \geq \cdots \geq a_{n-1}.\] Consider the following subsequences of \(s\):
\[s_1 \leftarrow ( \min(a_0, a_{n/2}), \min(a_1, a_{n/2+1}), \ldots, \min(a_{n/2-1}, a_{n-1}) ) \]
\[s_2 \leftarrow ( \max(a_0, a_{n/2}), \max(a_1, a_{n/2+1}), \ldots, \max(a_{n/2-1}, a_{n-1}) ) \]
a comparator network that takes as an input a bitonic sequence and performs a sequence of bitonic splits to sort it
key idea
use successively larger bitonic networks to transform the set into a bitonic sequence
in other words the depth \(d(n)\) of the network?
\[ d(n) = d(n/2) + \log n = \mathcal{O}(\log^2 n)\]
bitonic [b1, b2] = split (bitonic b) {
n = length(b);
for (i=0; i<n/2; ++i) {
b1[i] = min( b[i], b[i+n/2] );
b2[i] = max( b[i], b[i+n/2] );
}
}
bitonic [b1, b2] = reverse_split (bitonic b) {
[b2, b1] = split(b);
}
sequence s = sort_bitonic(bitonic b) {
[b1, b2] = split(b);
s = [sort_bitonic(b1), sort_bitonic(b2)];
}
sequence s = reverse_sort_bitonic(bitonic b) {
[b1, b2] = reverse_split(b);
s = [reverse_sort_bitonic(b1), reverse_sort_bitonic(b2)];
}
bitonic b = make_bitonic(sequence a) {
a1 = a[ 0,...,n/2-1];
a2 = a[n/2,...,n-1];
b1 = make_bitonic(a1);
b2 = make_bitonic(a2);
b = merge_bitonic(b1,b2);
}
bitonic b = merge_bitonic(b1, b2) {
b1 = sort_bitonic(b1);
b2 = reverse_sort_bitonic(b2);
b = [b1, b2];
}
communication characteristics of bitonic sort on a hypercube. during each stage of the algorithm, processes communicate along the dimensions shown.
function y = hcube_bitonic_sort(p, id, x)
d = log2(p);
for i=0:d-1
for j=i:-1:0
partner = flip_bit(id, j) % id xor 2^j
if id.bit(i+1) == id.bit(j)
comp_exchange_min (x, partner);
else
comp_exchange_max (x, partner);
end
end
end
end