ArticleslgStudy

computer science

Bitonic sorter

Bitonic sorter is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Bitonic sorter rather than just read about it. In short: Bitonic mergesort is a parallel algorithm for sorting. It is also used as a construction method for building a sorting network.

Bitonic sorter — main illustration
Bitonic sorter — illustration

Key takeaways

  • Bitonic sorter belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Bitonic sorter to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Bitonic sorter from memory before moving on to harder problems.

Reference excerpt

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 𝒪(n(log⁡n)2) comparators and have a delay of 𝒪((log⁡n)2), where n 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. 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 for which x0≤⋯≤xm≥⋯≥xn−1. 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 is a perfect power of two. We will therefore let k=log2(n) be the integer for which n=2k, meaning that the bitonic sorters may be enumerated in order of increasing size by considering the successive values k=1,2,3,….

Bitonic Sorter

A bitonic sorter for k=1 (n=2) is simply a comparator. 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, we can recursively create a sorter of higher order. For example, consider the following k=2 (n=4) bitonic sorter.

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, 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 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.

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.

Correctness of the bitonic sorter Ken Batcher provided some mathematical proof sketch in his paper. Without loss of generality the bitonic input sequence is assumed to be a1≤a2≤⋯≤aj−1≤aj≥aj+1≥⋯≥a2n with 1≤j≤2n. With out loss of generality the sequence can be reversed therefore, we can assume n≤j≤2n. Case 1: If an≤a2n then every element of the two subsequences are smaller. In this case di=ai and ei=an+i with 1≤i≤n and therefore di and ei are trivially bitonic. Case 2: Otherwise there exists a k such that the element ak of the first sub-sequence is bigger than ak of the second sub-sequence while it is the opposite for ak+1. This means that ak≤ak+n and ak+1>ak+n+1 are true for a specific k. Therefore, we now know that: 1. For 1≤i≤k the sequences are di=ai and ei=an+i 2. For k<i≤2n the sequences are defined as the opposite of 1, with di=an+i and ei=ai In the original paper he then claims the following inequalities result from those definitions: Following from 1:

For 1≤i≤k that di≤di+1 For j−n≤i≤k that ei≥ei+1 For 1≤i≤j−n that ei≤ei+1 Following from 2:

For k<i≤2n that di≥di+1 For k<i≤2n that ei≤ei+1 From both: en≤e1 From the paper claims follows that the sequences di and ei are in fact bitonic.

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 bitonic sort network.

The bitonic sorting network for k=2 can be created by using a k=2 bitonic sorter and two k=kprev−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 networks; hence, they are just a comparator. The following figure shows the overall scheme.

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)

… excerpt ends here. Continue reading the full article.

Illustrations

Bitonic sorter illustration
Bitonic sorter: A normal comparator with two inputs
A normal comparator with two inputs
Bitonic sorter: A n=4 (k=2) bitonic merge sorter
A n=4 (k=2) bitonic merge sorter
Bitonic sorter: test
test
Bitonic sorter: test
test

Worked examples

Example 1 — a first encounter with Bitonic sorter

Start with the simplest possible case. Write down what Bitonic sorter claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Bitonic sorter before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Bitonic sorter ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Bitonic sorter

In research
Bitonic sorter appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Bitonic sorter in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Bitonic sorter is common in secondary-school and first-year university syllabi. It links to neighbouring topics Sorting algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Bitonic sorter outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Bitonic sorter” →

How to study Bitonic sorter in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Bitonic sorter means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Bitonic sorter out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Bitonic sorter in simple terms?

Bitonic mergesort is a parallel algorithm for sorting. It is also used as a construction method for building a sorting network.

Why does Bitonic sorter matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Bitonic sorter?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Bitonic sorter.

Tags

  • Sorting algorithms

Keep exploring