Batcher odd-even merge network: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Execute a fixed comparator schedule independent of input values. Odd-even merge stages combine sorted subsequences with compare-exchange gates.

Double the merge width and refine odd-even merge strides. Comparators connect only positions belonging to the same merge block.

Open the 3D visualization

Batcher odd-even merge network example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Batcher odd-even merge 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.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        9,
        1,
        7,
        3,
        0,
        2,
        1,
        8
      ],
      "active": []
    }
  2. Step 5: Merge width 2, stride 2: compare 0 and 2 ascending; retain.

    State at this checkpoint
    {
      "values": [
        1,
        9,
        3,
        7,
        0,
        2,
        1,
        8
      ],
      "active": [
        0,
        2
      ],
      "comparator": [
        0,
        2
      ],
      "ascending": true
    }
  3. Step 10: Merge width 2, stride 1: compare 5 and 6 ascending; exchange.

    State at this checkpoint
    {
      "values": [
        1,
        3,
        7,
        9,
        0,
        1,
        2,
        8
      ],
      "active": [
        5,
        6
      ],
      "comparator": [
        5,
        6
      ],
      "ascending": true
    }
  4. Step 15: Merge width 4, stride 2: compare 2 and 4 ascending; exchange.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        1,
        8,
        2,
        3,
        7,
        9
      ],
      "active": [
        2,
        4
      ],
      "comparator": [
        2,
        4
      ],
      "ascending": true
    }
  5. Step 20: 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:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// 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 temp = 0;
    for (let width = 1; width < n; width *= 2) {
        for (let stride = width; stride > 0; stride >>= 1) {
            for (let j = stride % width; j + stride < n; j += 2 * stride) {
                for (let i = 0; i < stride; i++) {
                    if (((i + j) & ~(2 * width - 1)) == ((i + j + stride) & ~(2 * width - 1)) && a[i + j] > a[i + j + stride]) {
                        temp = a[i + j]; a[i + j] = a[i + j + stride]; a[i + j + stride] = 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.