Cycle sort: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Rotate permutation cycles into their final positions while handling duplicate values.

Count smaller remaining values to find a value's destination, then rotate displaced values through their placement cycle.

Open the 3D visualization

Cycle sort example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Cycle sort

Sorting when writes are particularly expensive.

Choosing an alternative

Selection sort also reduces exchanges but may rewrite positions.

Worked example: input and state changes

Example input

38, 12, 65, 8, 43, 27, 91, 19

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": [
        38,
        12,
        65,
        8,
        43,
        27,
        91,
        19
      ],
      "active": []
    }
  2. Step 4: Rotate the displaced item.

    State at this checkpoint
    {
      "values": [
        38,
        12,
        65,
        27,
        38,
        43,
        91,
        19
      ],
      "active": [
        3
      ]
    }
  3. Step 8: Write item into its cycle position.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        65,
        27,
        38,
        43,
        65,
        19
      ],
      "active": [
        6
      ]
    }
  4. Step 11: Count values smaller than 27; target position 3.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": [
        3,
        3
      ]
    }
  5. Step 15: Complete: values are in ascending order.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": []
    }

Expected final state

Complete: values are in ascending order.

Complexity and input bounds

Family: Sorting. Time complexity: O(n²).

Space complexity: O(1) auxiliary 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.

2–24 comma-separated integers between 0 and 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

Equal values require skipping occupied equal destinations to guarantee progress.

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
17
// Sorts a in place. n is the array length. Return value 0 indicates completion. 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) {
    for (let begin = 0; begin < n - 1; begin++) {
        let item = a[begin]; let pos = begin;
        for (let i = begin + 1; i < n; i++) { if (a[i] < item) { pos++; } }
        if (pos == begin) { continue; }
        while (a[pos] == item) { pos++; }
        let t = a[pos]; a[pos] = item; item = t;
        while (pos != begin) {
            pos = begin;
            for (let i = begin + 1; i < n; i++) { if (a[i] < item) { pos++; } }
            while (a[pos] == item) { pos++; }
            t = a[pos]; a[pos] = item; item = t;
        }
    }
    return 0;
}

Source

Haddon describes cycle-sort methods with conditional linear-time behavior. The playground uses a comparison-based quadratic write-minimizing variant.