Published by Dimitris Kougioumtzis · Updated
Execute a fixed comparator schedule independent of input values. Alternating ascending and descending runs form bitonic sequences before each merge.
Double the run width and halve the compare-exchange stride. XOR chooses each wire partner; the width bit determines whether a local sequence is ascending or descending.
When to use Bitonic sorting network
Teaching data-independent compare-exchange schedules and hardware-friendly parallel sorting designs.
Choosing an alternative
Both Batcher constructions use O(n * log^2(n)) comparators; adaptive insertion sort can exploit nearly sorted input, while the network schedule stays fixed.
Worked example: input and state changes
Example input
9,1,7,3,0,2,1,8
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initial state.
State at this checkpoint
{ "values": [ 9, 1, 7, 3, 0, 2, 1, 8 ], "active": [] }Step 6: Width 4, stride 2: compare 1 and 3 ascending; exchange.
State at this checkpoint
{ "values": [ 1, 3, 7, 9, 0, 2, 8, 1 ], "active": [ 1, 3 ], "comparator": [ 1, 3 ], "ascending": true }Step 13: Width 8, stride 4: compare 0 and 4 ascending; retain.
State at this checkpoint
{ "values": [ 1, 3, 7, 9, 8, 2, 1, 0 ], "active": [ 0, 4 ], "comparator": [ 0, 4 ], "ascending": true }Step 19: Width 8, stride 2: compare 4 and 6 ascending; exchange.
State at this checkpoint
{ "values": [ 1, 0, 1, 2, 7, 3, 8, 9 ], "active": [ 4, 6 ], "comparator": [ 4, 6 ], "ascending": true }Step 25: Fixed network complete; values are sorted.
State at this checkpoint
{ "values": [ 0, 1, 1, 2, 3, 7, 8, 9 ], "active": [] }
Expected final state
Fixed network complete; values are sorted.
Complexity and input bounds
Family: Sorting. Time complexity: O(n * log^2(n)).
Implementation detail: The view highlights each comparator and the exchanged bars in sequence. It executes the fixed network serially; it does not claim hardware parallel execution or a circuit wiring diagram.
Space complexity: O(1) auxiliary space for the in-place iterative download; saved visualization frames use additional space.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
Exactly 2, 4, 8 or 16 comma-separated integers, each 0..99.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
These implementations require power-of-two lengths, accept duplicates, and do not guarantee stable ordering of records with equal keys. Do not confuse comparator count with parallel depth.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 | // Sorts a in place and returns 0. n must be exactly 2, 4, 8 or 16; values 0..99. Fixed Batcher comparator schedule; O(n * log^2(n)) work, O(1) auxiliary space. Equal-value records are not guaranteed stable. Sequential function execution does not simulate parallel hardware depth. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(a, n) { let j = 0; let temp = 0; for (let width = 2; width <= n; width *= 2) { for (let stride = width >> 1; stride > 0; stride >>= 1) { for (let i = 0; i < n; i++) { j = i ^ stride; if (j > i && (((i & width) == 0 && a[i] > a[j]) || ((i & width) != 0 && a[i] < a[j]))) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } return 0; } |
Source
Original research on Batcher sorting networks. The playground executes a bounded iterative comparator schedule sequentially and reports work separately from parallel network depth.
- Sorting networks and their applications
Kenneth E. Batcher (1968). Original research paper.