The sorting network bitonic sort consists of Θ(n·log(n)2) comparators. It has the same asymptotic complexity as odd-even mergesort and shellsort. Although a sorting network with only O(n·log(n)) comparators is known [AKS 83], due to its large constant it is slower than bitonic sort for all practical problem sizes.

In the following, bitonic sort is developed on the basis of the 0-1-principle[Knu 73][CLRS 01]. The 0-1-principle states that a comparator network that sorts every sequence of 0's and 1's is a sorting network, i.e. it sorts every sequence of arbitrary values.

Theorem: Let a = a0, ..., an-1 be a bitonic 0-1-sequence, where n, n even. Application of comparator network Bn to a yields

Bn(a) = b0, ..., bn/2-1c0, ..., cn/2-1

where all bi are less than or equal to all cj , i.e.

bicj for all i, j {0, ..., n/2-1}

and furthermore

b0, ..., bn/2-1 is bitonic and c0, ..., cn/2-1 is bitonic.

Proof: Let a = a0, ..., an-1 be a bitonic 0-1-sequence. Written in two rows, the sequence looks like shown in the following figure. The sequence starts with 0's, continues with 1's, and ends with 0's (Figure 3a). Or it starts with 1's, continues with 0's, and ends with 1's (Figure 3b). The regions of 1's may overlap or not.

(a)

(b)

Figure 3: Bitonic 0-1-sequences (arranged in two rows)

Several other variations are possible (see Figure 4 below). Application of comparator network Bn corresponds to a comparison between upper and lower row. In each case, the result stated in the theorem is achieved: all bi are less than or equal to all cj and b is bitonic and c is bitonic (Figure 4):

The building blocks of the sorting network BitonicSort are comparator networks Bk with different k, where k is a power of 2. By using the divide-and-conquer strategy, networks BitonicMerge and BitonicSort are formed.

First, a comparator network BitonicMerge is built that sorts a bitonic sequence. Due to the theorem, Bn produces two bitonic subsequences, where all elements of the first are smaller or equal than those of the second. Therefore, BitonicMerge can be built recursively as shown in Figure 5.

The bitonic sequence necessary as input for BitonicMerge is composed of two sorted subsequences, where the first is in ascending and the other in descending order. The subsequences themselves are sorted by recursive application of BitonicSort (Figure 6).

Figure 5: BitonicMerge(n)

Figure 6: BitonicSort(n)

In the following Figure 7, as an example the sorting network BitonicSort(8) is given. The bitonic sequence e in the middle will be sorted by recursive application of Bk. The sequence e is bitonic, since it is composed of two halves d and d' which are sorted in opposite directions. These again are formed from bitonic sequences a and a' etc.

Given an arbitrary 0-1-sequence as input to the comparator network, the assertions stated in the figure will hold.

Figure 7: Sorting network BitonicSort for n = 8

At the output of the comparator network, the 0-1-sequence is sorted. Now the 0-1-principle can be applied to the comparator network as a whole: since the network sorts every arbitrary 0-1-sequence, it will also sort every sequence of arbitrary values, hence it is a sorting network.

In order to form a sorted sequence of length n from two sorted sequences of length n/2, there are log(n) comparator stages required (e.g. the 3 = log(8) comparator stages to form sequence i from d and d'). The number of comparator stages T(n) of the entire sorting network is given by:

Function bitonicSort first produces a bitonic sequence by recursively sorting its two halves in opposite directions, and then calls bitonicMerge.

Function bitonicMerge recursively sorts a bitonic sequence in ascending order, if dir = ASCENDING, and in descending order otherwise. The sequence to be sorted starts at index position lo, the number of elements is n.

A comparator is modelled by the function compare, where the parameter dir indicates the sorting direction. If dir is ASCENDING and a[i] > a[j] is true or dir is DESCENDING and a[i] > a[j] is false then a[i] and a[j] are exchanged.

We have presented the bitonic sorting network and proved its correctness. With Θ(n·log(n)2) comparators this sorting algorithm is not optimal – the lower bound for sorting based on comparisons is in Ω(n·log(n)). There are sorting algorithms like e.g. heapsort that achieve this lower bound. Nevertheless bitonic sort is interesting for realization in hardware or parallel processor arrays, since its sequence of comparisons is fixed and not dependent on data. The algorithm can be adapted to arbitrary n being not necessarily a power of 2 (bitonic sort for arbitrary n).