Bitonic sequence and sorting

Raj Upadhyay
13 min readDec 9, 2022

What are Bitonic Sequences?

A bitonic sequence is a sequence of sequence of elements {a0,a1, …..an-1} such that (1) there exist an index i such as {a0,a1, …..ai } is monotonically increasing and {ai , …..an-1} is monotonically decreasing or (2) there exist a cyclic shift of indices so (1) is satisfied.

– 〈1,2,4,7,6,0〉 is a bitonic sequence, because it first increases and then decreases. — 〈8,9,2,1,0,4〉 is another bitonic sequence, because it is a cyclic shift of 〈0,4,8,9,2,1〉.

Cyclic Shift of bitonic sequence is also a bitonic sequence.

Let s = 〈a0,a1,…,an-1〉 be a bitonic sequence such that a0 ≤ a1 ≤ ··· ≤ an/2–1 and an/2 ≥ an/2+1 ≥ ··· ≥ an-1. • Consider the following subsequences of s: s1 = 〈min{a0,an/2},min{a1,an/2+1},…,min{an/2–1,an-1}〉 s2 = 〈max{a0,an/2},max{a1,an/2+1},…,max{an/2–1,an-1}〉

Sorting Networks Bitonic Sort

Note that s1 and s2 are both bitonic and each element of s1 is less than every element in s2.

We can apply the procedure recursively on s1 and s2 to get the sorted sequence.

A bitonic merging network is a type of sorting network that is used to sort an array of numbers. It is based on the bitonic merge algorithm, which is a divide and conquer method for sorting an array. The network consists of log n columns, where n is the size of the input array. Each column contains n/2 comparators and performs one step of the bitonic merge. The comparators in each column compare pairs of items and swap them if necessary so that the larger item is always on the right.

How Bitonic Sort works ?

The first step of the bitonic merge is to divide the array in half and compare the elements in each half. The larger element is then placed in the first half and the smaller element is placed in the second half. This process is repeated until the array is sorted. The bitonic merging network uses this approach to sort the array. At each step, the comparators compare pairs of elements and swap them so that the larger element is always on the right. The sorting process is repeated until the array is completely sorted.

The bitonic merge algorithm is a simple and efficient sorting algorithm that is well-suited for hardware implementations. It can be used to sort large arrays quickly and with minimal memory usage. Furthermore, it can be used in parallel, allowing it to take advantage of modern hardware architectures. This makes it a popular choice for many sorting applications.

How do we sort an unsorted sequence using a bitonic merge?

A bitonic sequence is a sequence of numbers in which the elements are either in increasing or decreasing order. A single bitonic sequence can be built from a given sequence of numbers by sorting the elements in either increasing or decreasing order.

The simplest bitonic sequence is one of length 2, which is simply a sequence of two numbers that are either in increasing or decreasing order. To build a bitonic sequence of length 4, the first two elements of the given sequence can be sorted using ⊕BM[2], while the next two elements can be sorted using ӨBM[2]. This process can be repeated to create larger bitonic sequences.

For example, if the given sequence is [4, 7, 2, 5], the bitonic sequence can be built by sorting the first two elements of 4 and 7 using ⊕BM[2], and the next two elements of 2 and 5 using ӨBM[2], resulting in the bitonic sequence [4, 7, 5, 2].

By repeating this process, larger bitonic sequences can be constructed from the given sequence. This technique is useful for sorting large sequences of numbers, as it can efficiently build a bitonic sequence that can be sorted with a single pass.

Mapping Bitonic Sort to Hypercubes

The Bitonic Sort algorithm is a sorting algorithm that can be used to efficiently sort data in a distributed computing environment. It can be implemented on various architectures such as hypercubes, which are networks of processors that are interconnected in a way that the data can be transferred quickly and reliably. In order to map the Bitonic Sort algorithm to hypercubes, we must consider the case of one item per processor. In this case, the question becomes one of how the wires in the bitonic network should be mapped to the hypercube interconnect.

It is important to note that when implementing the Bitonic Sort algorithm on a hypercube, the compare-exchange operation is only performed between two wires if their labels differ in exactly one bit. This implies a direct mapping of wires to processors. All communication is nearest neighbour, meaning that each node is connected to its closest neighbours in the hypercube network. This allows the data to be transferred quickly and reliably, and the algorithm can be implemented in a distributed computing environment.

The bitonic sorting network is a useful tool for sorting data in an efficient manner. It is based on the idea of having a number of processors connected in a network, with each processor having a single data item. The bitonic network is composed of wires that connect the processors together. Each wire is labeled with a single bit, which indicates the relationships between the processors connected by that wire.

When the bitonic sorting network is mapped to a hypercube, the question is how the wires in the bitonic network should be mapped to the hypercube interconnect. A key observation is that the compare-exchange operation is only performed between two wires if their labels differ in exactly one bit. This implies a direct mapping of wires to processors, with all communication being nearest neighbour. This allows for efficient communication between the processors in the hypercube since the number of steps required to transmit data is minimized. Additionally, the bitonic network can be rearranged to take advantage of any special properties of the hypercube, such as its ability to communicate in multiple directions simultaneously.

In this example, we have a network that is designed to convert an input sequence of size n, into a bitonic sequence. The network consists of two types of bitonic merging networks (⊕BM[k] and ӨBM[k]) that are used to sort the input sequence.

The first step of the network is to divide the input sequence into two smaller sequences of size n/2. These two sequences are then sorted using the bitonic merging networks ⊕BM[n/2] and ӨBM[n/2]. This step is repeated recursively, until we have n/2 sequences of size 2, which are all sorted using the bitonic merging network ⊕BM[2].

Once the sequences have been reduced to size 2, they are combined together using the bitonic merging networks ⊕BM[4], ӨBM[4], ⊕BM[8], ӨBM[8], etc. until we reach the last merging network ⊕BM[16], which sorts the entire input sequence.

The benefit of this network is that it allows us to sort the input sequence in a much more efficient manner than traditional sorting algorithms. Furthermore, the network can be adapted to sort larger input sequences by simply increasing the size of the merging networks.

A comparator network is a type of sorting algorithm that can be used to transform a sequence of unordered numbers into a bitonic sequence. A bitonic sequence is one that is first increasing, then decreasing. To do this, the comparator network uses a series of comparison operations to compare two adjacent numbers and exchange them if they are out of order. This process is repeated until the entire sequence is in ascending order.

The comparator network for a 16-element sequence can be represented as a binary tree, where each node corresponds to a comparison operation between two elements. The leaves of the tree represent the 16 elements, while the root of the tree is the output of the bitonic sequence.

To create the comparator network, each comparison operation is performed in a series of steps. First, the two elements are compared and the larger one is placed on the right side of the tree; if they are equal, the left element is placed on the right side. Then, the two elements that were just compared are swapped if they are out of order. This process is repeated until the entire sequence is in ascending order.

The comparison operations are performed from the leaves to the root of the tree. This ensures that all elements are compared in the correct order and that the resulting bitonic sequence is correct. The comparator network for a 16-element sequence can be implemented using a series of 16 comparison operations, with each node in the tree representing one comparison.

By using a comparator network, we can quickly and easily transform an input sequence of unordered numbers into a bitonic sequence. This can be used in a variety of applications, such as sorting algorithms and data mining.

Bitonic Sort: Complexity

Bitonic Sort is an efficient sorting algorithm that is based on the “divide-and-conquer” paradigm. It is particularly suited for implementation on parallel architectures due to its relatively low depth and communication requirements. The algorithm is a comparison-based sorting algorithm and can be used to sort any collection of objects that can be compared.

The basic structure of the Bitonic Sort algorithm is a network of comparators. The comparators are arranged in a log2N depth, where N is the number of elements to be sorted. At each level of the network, the comparators compare the elements in pairs, swapping them if the elements are in the wrong order. This process continues until all elements are in the correct order.

The serial implementation of the Bitonic Sort algorithm has complexity Θ(nlog2n). This is because each stage of the network contains n/2 comparators and each comparator requires log2n comparisons. Therefore, the total number of comparisons required is nlog2n.

The parallel implementation of the Bitonic Sort algorithm has complexity Θ(log2n). This is because each stage of the network contains n/2 comparators and each comparator requires log2n comparisons. Therefore, the total number of comparisons required is log2n.

The advantage of the Bitonic Sort algorithm is its relatively low depth and communication requirements. This makes it well suited for implementation on parallel architectures. Furthermore, the algorithm can be adapted to suit a variety of architectures and can be used to sort any collection of objects that can be compared.

Mapping Bitonic Sequence to HyperCubes

Bitonic Sort on Hypercubes

Hypercubes are a type of parallel architecture consisting of processors connected by links. These processors can communicate with each other through a nearest-neighbor communication model. Bitonic Sort is an efficient algorithm for sorting data in a distributed environment. It can be mapped to hypercubes, allowing it to take advantage of the distributed nature of the architecture.

The Bitonic Sort algorithm works by first dividing the data into two parts and sorting each part in ascending (or descending) order. This is done by performing a series of compare-exchange operations between neighboring processors. After each part is sorted, the data is merged together, again using compare-exchange operations. This process is repeated until the data is completely sorted.

The time complexity of Bitonic Sort on a hypercube depends on the number of processors, n. Each step of the algorithm requires every process to perform a compare-exchange operation (single nearest neighbor communication of one word). Since each step takes Θ(1) time, the parallel time is Tp = Θ(log2n). This means that the algorithm can sort data in a distributed environment in O(log2n) time, making it an efficient sorting algorithm for parallel architectures.

The Bitonic Sort algorithm is an example of how algorithms can be adapted to take advantage of the distributed nature of hypercubes. By mapping the algorithm to the hypercube architecture, it is possible to achieve an efficient sorting algorithm with a low time complexity. This makes it an attractive option for distributed computing systems that need to sort large amounts of data.

Mapping Bitonic Sequence to Mesh

When mapping the bitonic sort algorithm to meshes, it is important to consider the connectivity of the mesh, which is lower than the connectivity of a hypercube. This means that there will likely be an overhead in the mapping process, as some of the operations required for the bitonic sort algorithm may need to be done in more time-consuming ways on a mesh architecture.

One way of dealing with this overhead is to use a row-major shuffled mapping of wires to processors. This involves assigning wires to processors in a row-major order, with each row having its own set of processors. This ensures that when processing the data, each processor can access the data in the same row without having to communicate with other processors that are outside of its communication range. This shuffled mapping helps to reduce the overhead of the mapping process, as the data can be processed more quickly and efficiently due to the lower communication requirements of the mesh architecture.

Different ways of mapping the input wires of the bitonic sorting network to a mesh of processes:

A) Row-Major Mapping:

Row-major mapping is the process of assigning each input wire of a bitonic sorting network to one process in a mesh of processes. In this arrangement, the processes are arranged in a row-major order, so that the processes are laid out in the same order as the data is laid out in a row. In this way, each process can access the data in its row and can exchange data with its neighbours. This type of mapping is often used when the data is laid out in a row with a known ordering.

B) Row-Major Snakelike Mapping:

Row-major snakelike mapping is a variation on the row-major mapping. Instead of assigning the input wires to processes in a straight line, the input wires are assigned in a “snakelike” pattern. This means that instead of assigning the input wires to processes in a straight line, the wires are assigned in a serpentine pattern. This type of mapping can be used to improve the performance of the sorting network by allowing for more efficient data exchange between processes.

C) Row-Major Shuffled Mapping:

Row-major shuffled mapping is a variation on the row-major mapping. Instead of assigning the input wires to processes in a straight line, the input wires are assigned in a random order. This random order can help to improve the performance of the sorting network by allowing more efficient data exchange between processes. Furthermore, this type of mapping can also help to reduce the number of active processes in the mesh, as some processes may not need to be used.

The bitonic sort algorithm is used to arrange a list of numbers in either ascending or descending order. To map the bitonic sort algorithm to a mesh with 16 processes, we will use a row-major shuffled mapping. This means that the processes are arranged in a 4 x 4 grid, beginning with the process in the upper left corner and proceeding in the row-major order to the process in the lower right corner.

The bitonic sort algorithm requires that the processes compare and exchange their elements in pairs. The following diagram shows the pairs of processes that will perform the compare-exchange operations, as indicated by arrows.

Process 0 will exchange elements with process 1, process 2 will exchange elements with process 3, and so on, until process 14 exchanges elements with process 15.

Process 0 will also exchange elements with process 4, process 1 with process 5, and so on, until process 11 exchanges elements with process 15.

Process 0 will then exchange elements with process 8, process 1 with process 9, and so on, until process 7 exchanges elements with process 15.

Finally, process 0 will exchange elements with process 12, process 1 with process 13, and so on, until process 3 exchanges elements with process 15.

This is the row-major shuffled mapping of the bitonic sort algorithm for n = 16 on a mesh.

The bitonic sort is a sorting algorithm used to sort a sequence of numbers in either ascending or descending order. It is based on a comparison-based sorting algorithm and works by splitting a given set of numbers into two sub-arrays and then sorting each of these sub-arrays separately. The two sub-arrays are then merged together in a sorted fashion.

The bitonic sort can be mapped to a mesh architecture, by assigning each process in the mesh to a bit in the sequence. In the row-major shuffled mapping, wires that differ at the i th least-significant bit are mapped onto mesh processes that are 2⎣(i-1)/2⎦ communication links away. This means that each process needs to communicate with two other processes for each bit in the sequence.

The total amount of communication performed by each process is O (log2n). The total computation performed by each process is also O(log2n). As a result, the parallel runtime of the bitonic sort on a mesh architecture is O(log2n).

However, this is not cost optimal, since the total number of communication links is much higher than what is required to sort the array. To make it cost-optimal, one needs to use a better mapping of the array onto the mesh, or use a better sorting algorithm.

Block of Elements Per Processor

The bitonic sort algorithm is a parallel sorting algorithm that can be used to efficiently sort an array of elements. It is particularly suited for meshes because it is an efficient way of sorting data in a distributed environment.

The bitonic sort can be mapped to a mesh by assigning each process a block of n/p elements. The first step is to perform a local sort of the block assigned to each process. The next step is to perform a compare-exchange operation, which replaces each element with the larger of the two elements being compared. This operation is repeated until all elements in the array have been sorted.

At this point, the bitonic network has (1 + log p)(log p)/2 steps. This means that each process will compare its elements with the elements of other processes in the network. This process is repeated until the elements of all processes have been sorted.

The advantage of using the bitonic sort algorithm in a mesh is that it is a fast, efficient way to sort data in a distributed environment. It does not require a large amount of memory, and it can be adapted for sorting large amounts of data. Additionally, the algorithm is easily scalable, meaning it can be adapted for different sizes of meshes.

Block of Elements Per Processor: Hypercube

• Initially the processes sort their n/p elements (using

merge sort) in time Θ((n/p)log(n/p)) and then perform

Θ(log2p) compare-split steps.

• The parallel run time of this formulation is

• Comparing to an optimal sort, the algorithm can

efficiently use up to processes.

• The isoefficiency function due to both communication

and extra work is Θ(plog plog2p) .

The Bitonic Sort algorithm can be efficiently implemented on both hypercube and mesh architectures. It offers an efficient solution for sorting large datasets with a parallel run time of Θ((n/p)log(n/p)+log2p) and Θ((n/p)log(n/p)+log2b) respectively. The isoefficiency function due to both communication and extra work is Θ(plog plog2p) and Θ(b²log (b²)log2b) respectively. This makes Bitonic Sort a good choice for sorting large datasets in parallel.

--

--