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
- If the length is not a power of two, pad it with large values (this app's implementation; they are removed at the end).
- Recursively sort the first half ascending and the second half descending to form a bitonic sequence.
- 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.
| # | Operation | Array after |
|---|---|---|
| 1 | Compare [0]=3 and [1]=1 | [3, 1, 4, 2] |
| 2 | Swap [0]=3 and [1]=1 | [1, 3, 4, 2] |
| 3 | Compare [2]=4 and [3]=2 | [1, 3, 4, 2] |
| 4 | Compare [0]=1 and [2]=4 | [1, 3, 4, 2] |
| 5 | Compare [1]=3 and [3]=2 | [1, 3, 4, 2] |
| 6 | Swap [1]=3 and [3]=2 | [1, 2, 4, 3] |
| 7 | Compare [0]=1 and [1]=2 | [1, 2, 4, 3] |
| 8 | Compare [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.