日本語

PARALLEL-FRIENDLY STRUCTURES

Bitonic Sort, visualised

Bitonic sort builds a bitonic sequence — an ascending run joined to a descending run — and then sorts it by comparing and swapping halves. Its comparison pattern is fixed regardless of input, which suits parallel processing.

Run Bitonic Sort in the visualizer →

Opens the Sorting Cinema visualizer with 16 elements in English.

How it works

  1. If the length is not a power of two, pad it with large values (this app's implementation; they are removed at the end).
  2. Recursively sort the first half ascending and the second half descending to form a bitonic sequence.
  3. Compare and swap elements half the range apart, then repeat recursively on each half.

Complexity and stability

Best
O(n log² n)
Average
O(n log² n)
Worst
O(n log² n)
Extra space
O(log n) (recursion stack, excluding padding to a power of two)
Stability
Usually unstable

The number and pattern of comparisons do not depend on the input, so best, average and worst cases are all O(n log² n) (counting n after padding to a power of two). This app animates sequentially and does not measure parallel performance.

Source of facts: Complexity is derived from the steps of this app's implementation (src/SortingCinema.jsx).

Trace on a small array

The first operations on the input [3, 1, 4, 2], following the same steps as the app.

#OperationArray after
1Compare [0]=3 and [1]=1[3, 1, 4, 2]
2Swap [0]=3 and [1]=1[1, 3, 4, 2]
3Compare [2]=4 and [3]=2[1, 3, 4, 2]
4Compare [0]=1 and [2]=4[1, 3, 4, 2]
5Compare [1]=3 and [3]=2[1, 3, 4, 2]
6Swap [1]=3 and [3]=2[1, 2, 4, 3]
7Compare [0]=1 and [1]=2[1, 2, 4, 3]
8Compare [2]=4 and [3]=3[1, 2, 4, 3]

…and so on. In this trace, 9 compare / swap / write operations in total produce [1, 2, 3, 4].

Suitable inputs and caveats

  • Sorted and reversed inputs go through exactly the same comparison order.
  • Lengths that are not powers of two do extra work for the padded elements.
  • It swaps distant elements, so it is usually unstable.

FAQ

What is a bitonic sequence?

A sequence that first increases and then decreases (or can be rotated to do so). A bitonic sequence can be sorted by repeatedly comparing and swapping halves.

Why pad to a power of two?

The algorithm splits the range in half again and again. This app pads with large values and removes them after sorting.