# Bitonic Sorting

Thomas Anastasio December 4, 2003

1

Deﬁnitions

A monotonic sequence is one in which the values increase (or decrease) from left-to-right. The sequence a 1 , a2 , · · · , an is monotonic if ak < ak+1 for all k < n. A bitonic sequence is one that monotonically increases (decreases), reaches a single maximum (minimum), then monotonically decreases (increases). A sequence is also considered bitonic if it can be made so by cyclically shifting the sequence. For example, the two sequences 3 5 8 9 7 4 2 1 5 8 9 7 4 2 1 3 are bitonic. The ﬁrst one increases from 3 to 9, then decreases. The second one can be converted to the ﬁrst one by cyclically shifting. A bitonic split is an operation on a bitonic sequence such that if a i > ai+n/2 , the two elements are exchanged, 1 ≤ i < n. The operation produces two bitonic sequences A and B such that the elements in A are all less than the elements in B. For example, here is the split operation on a bitonic sequence of 8 elements. 0 4 4 1 2 3 8 12 15 6 3 2 Sequence A 4 11 11 5 6 7 6 3 2 8 12 15 Sequence B Indices Bitonic sequence Two bitonic sequences

Comparing index 0 with index 0 + 8/2, we ﬁnd that 4 is less than 11 and do not swap. Comparing index 1 with index 1 + 8/2, we do swap the 8 and 6 values. Perform this swapping operation for indices i = (0, n/2 − 1] to get the result shown. In this particular case, the left-half sequence increases, then decreases, while the right-half sequence does the opposite. This is not necessarily the outcome in general. The only guaranteed outcome is that the two sub-sequences will each be bitonic.

2

Sorting Bitonic Sequences

Bitonic sorting takes advantage of this property of the bitonic split. By repeated applications of the bitonic split, a bitonic sequence can be converted to a monotonic sequence (i.e., sorted). Here’s an example:

1

Basically. right down) Pairwise exchange alternating order Note that the arbitrary sequence of 8 elements was converted to a bitonic sequence in two steps: 1. 4 A Bigger Example Here we demonstrate bitonic sorting of a 16-element arbitrary sequence. · · · . then apply a sequence of bitonic splits. takes k steps. 2. The number of steps is in O(lgn). can be sorted in k steps. 3 Sorting Arbitrary Sequences To sort an arbitrary sequence. we did this one above. The ﬁrst step had one sub-step. as above. Each step has 1. The idea is to pairwise produce bitonic sequences of increasing size. n = 2 k . In general. The second step had two sub-steps. 2. This results in a 16-element bitonic sequence that we then sort in the usual way. It took 3 steps. the split and the exchange. As expected. k Thus. one increasing the other decreasing.4 4 3 2 8 6 2 3 12 3 4 4 15 2 6 6 11 11 11 8 6 8 8 11 3 12 12 12 2 15 15 15 Bitonic sequence First split Split each half Split each quarter (exchange) This sort of an 8-element bitonic sequence was done in three steps. we do two 8-element sorts. (4(4 + 1)/2). 3. k substeps. we ﬁrst make it into a bitonic sequence. 8 8 8 4 4 3 2 12 12 4 8 6 2 3 4 15 15 12 3 4 4 15 4 12 15 2 6 6 Now 6 1 3 4 2 11 6 3 5 14 2 11 6 3 14 5 6 11 2 3 14 16 11 6 3 2 16 14 11 8 12 15 16 14 11 8 12 15 16 14 8 11 12 15 16 14 have a single bitonic sequence Sort it bitonically 8 7 5 1 16 14 8 7 5 6 9 11 5 6 8 7 9 10 5 6 7 8 9 10 16 10 1 10 16 9 10 5 7 10 5 1 10 13 1 10 13 9 13 10 9 of 16 elements 13 12 12 11 10 10 11 12 9 16 13 13 9 1 1 7 7 7 7 13 7 9 9 9 1 5 7 13 13 13 5 5 1 Unsorted Pairwise Exchange Bitonic Split each half Pairwise Bitonic Sort each half 2 2 2 1 3 3 1 2 4 4 4 3 11 14 14 14 12 13 16 15 15 15 15 16 Bitonic Sort entire sequence . Here’s an example: 8 8 8 4 12 12 4 8 4 15 15 12 15 4 12 15 2 2 6 11 11 11 11 6 6 6 2 3 3 3 3 2 Arbitrary sequence Pairwise exchange alternating order Split each half (left up. the pairwise exchange. the sort of 24 elements takes 10 substeps. n = 2 k . an n-element bitonic sequence. the total number of substeps is i=1 i = k(k + 1)/2 = lg(n)(lg(n) + 1)/2 = O(lg 2 (n)). This leads to the following observation. To sort an arbitrary sequence of n-elements. We know how to sort a bitonic sequence.