Article · Wikipedia archive · Last revised Jun 7, 2026

Bitonic sorter

Bitonic mergesort is a parallel algorithm for sorting. It is also used as a construction method for building a sorting network. The algorithm was devised by Ken Batcher. The resulting sorting networks consist of comparators and have a delay of , where is the number of items to be sorted. This makes it a popular choice for sorting large numbers of elements on an architecture which itself contains a large number of parallel execution units running in lockstep, such as a typical GPU.

Last revised
Jun 7, 2026
Read time
≈ 9 min
Length
2,052 w
Citations
23
Source
Bitonic sorter
Bitonic sorting network (bitonic merge sort) with 4 inputs with an example sequence that is being sorted.
ClassSorting algorithm
Data structureArray
Worst-case performance O ( ( log n ) 2 ) {\displaystyle {\mathcal {O}}((\log n)^{2})} parallel time12
Best-case performance O ( ( log n ) 2 ) {\displaystyle {\mathcal {O}}((\log n)^{2})} parallel time 12
Average performance O ( ( log n ) 2 ) {\displaystyle {\mathcal {O}}((\log n)^{2})} parallel time12
Worst-case space complexity O ( n ( log n ) 2 ) {\displaystyle {\mathcal {O}}(n(\log n)^{2})} non-parallel time12
OptimalNo

Bitonic mergesort is a parallel algorithm for sorting. It is also used as a construction method for building a sorting network. The algorithm was devised by Ken Batcher.3 The resulting sorting networks consist of O ( n ( log n ) 2 ) {\displaystyle {\mathcal {O}}(n(\log n)^{2})} comparators and have a delay of O ( ( log n ) 2 ) {\displaystyle {\mathcal {O}}((\log n)^{2})} , where n {\displaystyle n} is the number of items to be sorted.12 This makes it a popular choice for sorting large numbers of elements on an architecture which itself contains a large number of parallel execution units running in lockstep, such as a typical GPU.

A sorted sequence is a monotone sequence---that is, a sequence which is either non-decreasing or non-increasing. A sequence is bitonic when it consists of a non-decreasing sequence followed by a non-increasing sequence, i.e. when there exists an index m {\displaystyle m} for which x 0 x m x n 1 . {\displaystyle x_{0}\leq \cdots \leq x_{m}\geq \cdots \geq x_{n-1}.} 3

A bitonic sorter can only sort inputs that are bitonic. Bitonic sorters can be used to build a bitonic sort network that can sort arbitrary sequences by using the bitonic sorter with a sort-by-merge scheme, in which partial solutions are merged using bigger sorters.

The following sections present the algorithm in its original formulation, which requires an input sequence whose length n {\displaystyle n} is a perfect power of two. We will therefore let k = log 2 ( n ) {\displaystyle k=\log _{2}(n)} be the integer for which n = 2 k {\displaystyle n=2^{k}} , meaning that the bitonic sorters may be enumerated in order of increasing size by considering the successive values k = 1 , 2 , 3 , {\displaystyle k=1,2,3,\ldots } .

Bitonic Sorter

This image shows a comparator with two inputs labeled X and Y. And two outputs H and L
A normal comparator with two inputs source ↗

A bitonic sorter for k = 1 {\displaystyle k=1} ( n = 2 ) {\displaystyle (n=2)} is simply a comparator.3 This is illustrated by the given box layout, in which X and Y represent the inputs, while H and L represent the higher and lower outputs, respectively.

With the sorter for k = 1 {\displaystyle k=1} , we can recursively create a sorter of higher order. For example, consider the following k = 2 {\displaystyle k=2} ( n = 4 ) {\displaystyle (n=4)} bitonic sorter.3

This image shows a bitonic merge sorter with 4 inputs. On the left are the inputs x1 to x4. These are connected to two comparators with x1 and x3 connected two one and x2 and x4 connected to the other. All low outputs go then into one comparator and all high outouts go into the other.
A n = 4 {\displaystyle n=4} ( k = 2 ) {\displaystyle (k=2)} bitonic merge sorter source ↗

The bitonic sorter consists of two layers: a recombination layer, which recombines the bitonic inputs into two new bitonic sequences that are each half as long as the original sequence, and a bitonic sort layer consisting of two bitonic sorters of order k 1 {\displaystyle k-1} , each of which sorts one of the two bitonic sequences produced by the previous layer. This structure may be extended recursively for higher values of k {\displaystyle k} by ensuring that each comparator always accepts one input from each of the two halves of the bitonic sequence it is meant to help sort. The following illustration depicts these connections schematically.3

test
test source ↗

As you can see, elements of the first half of the input sequence are pairwise compared against corresponding elements of the last half of the input sequence. Comparing each element of the (green) subsequence with the element of the other (orange) subsequence at the respective index produces two bitonic subsequences. These two bitonic series (blue and red, respectively) can then be fed into the next lower-order bitonic sorter. This can be done because all elements in the red sequence are guaranteed to be higher than all elements in the blue series. 3

Correctness of the bitonic sorter

Ken Batcher provided some mathematical proof sketch in his paper.3 With out loss of generality the bitonic input sequence is assumed to be a 1 a 2 a j 1 a j a j + 1 a 2 n {\displaystyle a_{1}\leq a_{2}\leq \dots \leq a_{j-1}\leq a_{j}\geq a_{j+1}\geq \dots \geq a_{2n}} with 1 j 2 n {\displaystyle 1\leq j\leq 2n} . With out loss of generality the sequence can be reversed therefore, we can assume n j 2 n {\displaystyle n\leq j\leq 2n} .

Case 1: If a n a 2 n {\displaystyle a_{n}\leq a_{2n}} then every element of the two subsequences are smaller. In this case d i = a i {\displaystyle d_{i}=a_{i}} and e i = a n + i {\displaystyle e_{i}=a_{n+i}} with 1 i n {\displaystyle 1\leq i\leq n} and therefore d i {\displaystyle d_{i}} and e i {\displaystyle e_{i}} are trivially bitonic.

Case 2: Otherwise there exists a k {\displaystyle k} such that the element a k {\displaystyle a_{k}} of the first sub-sequence is bigger than a k {\displaystyle a_{k}} of the second sub-sequence while it is the opposite for a k + 1 {\displaystyle a_{k+1}} . This means that a k a k + n {\displaystyle a_{k}\leq a_{k+n}} and a k + 1 > a k + n + 1 {\displaystyle a_{k+1}>a_{k+n+1}} are true for a specific k {\displaystyle k} . Therefore, we now know that:

1. For 1 i k {\displaystyle 1\leq i\leq k} the sequences are d i = a i {\displaystyle d_{i}=a_{i}} and e i = a n + i {\displaystyle e_{i}=a_{n+i}}

2. For k < i 2 n {\displaystyle k<i\leq 2n} the sequences are defined as the opposite of 1, with d i = a n + i {\displaystyle d_{i}=a_{n+i}} and e i = a i {\displaystyle e_{i}=a_{i}}

In the original paper he then claims the following inequalities result from those definitions:3

Following from 1:

  • For 1 i k {\displaystyle 1\leq i\leq k} that d i d i + 1 {\displaystyle d_{i}\leq d_{i+1}}
  • For j n i k {\displaystyle j-n\leq i\leq k} that e i e i + 1 {\displaystyle e_{i}\geq e_{i+1}}
  • For 1 i j n {\displaystyle 1\leq i\leq j-n} that e i e i + 1 {\displaystyle e_{i}\leq e_{i+1}}

Following from 2:

  • For k < i 2 n {\displaystyle k<i\leq 2n} that d i d i + 1 {\displaystyle d_{i}\geq d_{i+1}}
  • For k < i 2 n {\displaystyle k<i\leq 2n} that e i e i + 1 {\displaystyle e_{i}\leq e_{i+1}}

From both: e n e 1 {\displaystyle e_{n}\leq e_{1}}

From the paper claims follows that the sequences d i {\displaystyle d_{i}} and e i {\displaystyle e_{i}} are in fact bitonic.3

Bitonic Sorting Networks (Bitonic Merge Sort)

A bitonic sorting network is created by using several bitonic sorters. These bitonic sorters are recursively used to create two monotonic sequences, one decreasing and one increasing, which are then put into the next stage. This creates a bitonic series for the next stage, which can then use this bitonic series as a monotonic series for the next stage. Consider the following example for an n = 4 {\displaystyle n=4} bitonic sort network.3

testa
test source ↗

The bitonic sorting network for k = 2 {\displaystyle k=2} can be created by using a k = 2 {\displaystyle k=2} bitonic sorter and two k = k p r e v 1 {\displaystyle k=k_{prev}-1} sorters. The two sorters create a decreasingly or increasingly sorted sequence in order to create a bitonic input for the bitonic sorter. Bitonic sorting networks of a lower order are mostly used for the two pre-sorters; therefore, a recursive definition of a bitonic sorting network from bitonic sorters can be described. In the above example, the two bitonic sorting networks are k = 1 {\displaystyle k=1} networks; hence, they are just a comparator.3 The following figure shows the overall scheme.

testa
test source ↗

This overall scheme requires the sorter to have an input of sequence that is a power of two. There are, however, possibilities to mitigate this by, for example, using sentinel values.

Pseudocode

The following pseudocode describes the sorting process. In the code, a is the array to be sorted, low is the index of the first item in the sub-array to be sorted, k and count is the number items in the sub-array that are being sorted in this function call. direction is a boolean value that determines whether the sub-array is being sorted into ascending / descending order.

The function call bitonicSort(a, 0, n, 1) is used to sort a (ascending), where n is the number of items in a .

function bitonicMerge(a, low, count, direction) is
    if count > 1 THEN
        k ← count / 2
        // Compare and swap elements across the halves
        for i ← low to low + k do
            // determine if two elements of a are out of order in relation to the direction of sorting. 
            if (direction == 1 AND a[i] > a[i + k]) OR (direction == 0 AND a[i] < a[i + k]) THEN
                swap a[i] with a[i + k]

        // Recursively merge both halves
        bitonicMerge(a, low, k, direction)
        bitonicMerge(a, low + k, k, direction)

// This only works when input size is a power of 2.
function bitonicSort(a, low, count, direction) is
    if count > 1 THEN
        k ← count / 2

        // Sort first/second half into ascending/descending order
        bitonicSort(a, low, k, 1)
        bitonicSort(a, low + k, k, 0)

        // Merge entire sequence in desired order
        bitonicMerge(a, low, count, direction)

Complexity

In this section we assume that our sorter has n = 2 k {\displaystyle n=2^{k}} input elements as previously.

Each recursion in a bitonic sorting network adds a sorter of order k n e x t = k p r e v 1 {\displaystyle k_{next}=k_{prev}-1} , which consists of bitonic k n e x t {\displaystyle k_{next}} sorter and the next recursion. As both sub-sorters can be done in parallel, only one level is added for each level in both sub-sorters. Each bitonic sorter has, therefore, one recombination layer and a lower-order bitonic sorter for its recursion. This results in k {\displaystyle k} levels per bitonic sorter. Therefore, we can describe this construction's levels as the following sum: i = 1 k i {\displaystyle \sum _{i=1}^{k}i} .

This sum can be reduced using the Gauss sum formula i = 1 k i = 1 2 k ( k + 1 ) {\displaystyle \sum _{i=1}^{k}i={\dfrac {1}{2}}k(k+1)}

Therefore, the number of levels in which each comparison can be done in parallel is given by 1 2 k ( k + 1 ) {\displaystyle {\dfrac {1}{2}}k(k+1)} .3 Which gives us O ( k 2 + k ) = O ( k 2 ) = O ( ( log 2 n ) 2 ) {\displaystyle {\mathcal {O}}(k^{2}+k)={\mathcal {O}}(k^{2})={\mathcal {O}}((\log _{2}n)^{2})} assuming n {\displaystyle n} comparisons can be performed in parallel.

Although the absolute number of comparisons is typically higher than Batcher's odd-even sort, many of the consecutive operations in a bitonic sort retain a locality of reference, making implementations more cache-friendly and typically more efficient in practice.3

See also

See also

References

References

  1. Megha, Jain; Sanjay, Kumar; V.K, Patle (March 2015). "Bitonic Sorting Algorithm: A Review". International Journal of Computer Applications. 113 (13): 40–43. Bibcode:2015IJCA..113m..40J. doi:10.5120/19890-1930. Retrieved 14 May 2025.
  2. Ranković, Vukašin; Kos, Anton; Milutinović, Veljko (July 2013). "Bitonic Merge Sort Implementation on the Maxeler Dataflow Supercomputing System" (PDF). The IPSI BGD Transactions on Internet Research. 9 (2): 5–10. Retrieved 14 May 2025.
  3. Batcher, K. E. (30 April 1968). "Sorting networks and their applications". Proceedings of the April 30--May 2, 1968, spring joint computer conference on - AFIPS '68 (Spring). pp. 307–314. doi:10.1145/1468075.1468121.
External links