# Bitonic Sort in C++

The bitonic sort is a parallel sorting algorithm that is created for best implementation and has optimum usage with hardware and parallel processor array.

It is not the most effective one though as compared to the merge sort. But it is good for parallel implementation. This is due to the predefined comparison sequence which makes comparisons independent of data that are to be sorted.

For bitonic sort to work effectively the number of elements should be in a specific type of quantity i.e. the order 2^n.

One major part of the bitonic sort is the bitonic sequence which is a sequence whose elements value first increases and then decreases.

Bitonic sequence is an array arr[0 … (n-1)] if there is an index value i within the range 0 to n-1. For which the value of arr[i] is greatest in the array. i.e.

arr[0] <= arr[1] … <= arr[i] and arr[i] >= arr[i+1] … >= aar[n-1]

## Special Characteristics of bitonic sequence −

• Bitonic sequence can be rotated back to bitonic sequence.

• A sequence with elements in increasing and decreasing order is a bitonic sequence.

## Creating a bitonic sequence

For creating a bitonic sequence, we will create two sub-sequences, one with ascending elements and other with descending elements.

For example, let’s see the sequence arr[] and convert the following sequence to a bitonic sequence.

arr[] = {3, 4, 1, 9, 2, 7, 5, 6}

First, we will make pairs of elements and then create a bitonic sequence of these in such a way that one is in ascending order and the other is in descending order and so on.

For our array, let’s create pairs in bitonic sequence.

arr[] = {(3, 4), (1, 9), (2, 7), (5, 6)}
// creating bitonic sequence pairs…
arr[] = {(3, 4), (9, 1), (2, 7), (6, 5)}

Then, we will create pairs of these pairs. i.e 4 elements bitonic sequence and compare elements with are a 2 distance [i.e. i with i+2].

arr[] = {(3, 4, 9, 1), (2, 7, 6, 5)}

Ascending bitonic in first set will be created as −

(3, 4, 9, 1) : comparing two distant elements.
(3, 1, 9, 4) : Now, check adjacent elements.
(1, 3, 4, 9) -> ascending bitonic sequence.

Descending bitonic in second set will be created as −

(2, 7, 6, 5) : comparing two distant elements.
(6, 7, 2, 5) : Now, check adjacent elements.
(7, 6, 5, 2) -> descending bitonic sequence.

At the end, we will get the bitonic sequence of size 8.

1, 3, 4, 9, 7, 6, 5, 2

Now, since we have learned about the bitonic sequence. We will know about the Bitonic Sorting.

## BITONIC SORTING

To sort a bitonic sequence using bitonic sorting using these steps −

Step 1 − Create a bitonic sequence.

Step 2 − Now, we have a bitonic sequence with one part in increasing order and others in decreasing order.

Step 3 − We will compare and swap the first elements of both halves. Then second, third, fourth elements for them.

Step 4 − We will compare and swap, every second element of the sequence.

Step 5 − At last, we will compare and swap adjacent elements of the sequence.

Step 6 − After all swaps, we will get the sorted array.

## Example

Program to show the implementation of Bitonic Sort −

Live Demo

#include<iostream>
using namespace std;
void bitonicSeqMerge(int a[], int start, int BseqSize, int direction) {
if (BseqSize>1){
int k = BseqSize/2;
for (int i=start; i<start+k; i++)
if (direction==(a[i]>a[i+k]))
swap(a[i],a[i+k]);
bitonicSeqMerge(a, start, k, direction);
bitonicSeqMerge(a, start+k, k, direction);
}
}
void bitonicSortrec(int a[],int start, int BseqSize, int direction) {
if (BseqSize>1){
int k = BseqSize/2;
bitonicSortrec(a, start, k, 1);
bitonicSortrec(a, start+k, k, 0);
bitonicSeqMerge(a,start, BseqSize, direction);
}
}
void bitonicSort(int a[], int size, int up) {
bitonicSortrec(a, 0, size, up);
}
int main() {
int a[]= {5, 10, 51, 8, 1, 9, 6, 22};
int size = sizeof(a)/sizeof(a[0]);
printf("Original array: \n");
for (int i=0; i<size; i++)
printf("%d\t", a[i]);
bitonicSort(a, size, 1);
printf("\nSorted array: \n");
for (int i=0; i<size; i++)
printf("%d\t", a[i]);
return 0;
}

## Output

Original array:
5 10 51 8 1 9 6 22
Sorted array:
1 5 6 8 9 10 22 51