Suffix array (prefix doubling): Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Sort suffixes by doubling the ranked prefix length. ASCII ordinal order is used.

Sort suffix rank pairs while repeatedly doubling the known prefix length, refining suffix order without whole-suffix comparisons.

Open the 3D visualization

Suffix array (prefix doubling) example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Suffix array (prefix doubling)

String indexing and LCP preprocessing.

Choosing an alternative

Naive suffix sorting compares full strings; advanced constructions can offer better bounds.

Worked example: input and state changes

Example input

banana

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": [
        0,
        1,
        2,
        3,
        4,
        5
      ],
      "active": []
    }
  2. Step 3: Sort rank pairs of width 1.

    State at this checkpoint
    {
      "values": [
        1,
        3,
        0,
        2,
        4,
        5
      ],
      "active": [
        1,
        2
      ],
      "suffixes": [
        "anana",
        "ana",
        "banana",
        "nana",
        "na",
        "a"
      ]
    }
  3. Step 7: Sort rank pairs of width 1.

    State at this checkpoint
    {
      "values": [
        1,
        5,
        3,
        0,
        2,
        4
      ],
      "active": [
        1,
        2
      ],
      "suffixes": [
        "anana",
        "a",
        "ana",
        "banana",
        "nana",
        "na"
      ]
    }
  4. Step 10: Sort rank pairs of width 2.

    State at this checkpoint
    {
      "values": [
        5,
        3,
        1,
        0,
        2,
        4
      ],
      "active": [
        1,
        2
      ],
      "suffixes": [
        "a",
        "ana",
        "anana",
        "banana",
        "nana",
        "na"
      ]
    }
  5. Step 13: Suffix array complete. Values are suffix start indices.

    State at this checkpoint
    {
      "values": [
        5,
        3,
        1,
        0,
        4,
        2
      ],
      "active": [],
      "suffixes": [
        "a",
        "ana",
        "anana",
        "banana",
        "na",
        "nana"
      ]
    }

Expected final state

Suffix array complete. Values are suffix start indices.

Complexity and input bounds

Family: Strings. Time complexity: O(n² log n) with insertion sorting.

Space complexity: O(n) indices, ranks, and temporary ranks.

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.

One string of 1–24 ASCII letters.

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

This reference uses insertion sorting for rank pairs, so its bound differs from faster sorting variants.

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
18
19
20
21
22
23
24
25
// 1–24 ASCII letter codes. Writes sorted suffix start indices. Prefix doubling; C-family versions use insertion sort, Python uses native stable sorting. 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, output) {
    let rank = Array(256).fill(0); let next = Array(256).fill(0);
    for (let i = 0; i < n; i++) { output[i] = i; rank[i] = a[i]; }
    for (let width = 1; width < n; width *= 2) {
        for (let i = 1; i < n; i++) {
            let j = i;
            while (j > 0) {
                let x = output[j - 1]; let y = output[j]; let secondX = -1; let secondY = -1;
                if (x + width < n) { secondX = rank[x + width]; } if (y + width < n) { secondY = rank[y + width]; }
                if (rank[x] < rank[y] || (rank[x] == rank[y] && secondX <= secondY)) { break; }
                output[j - 1] = y; output[j] = x; j--;
            }
        }
        next[output[0]] = 0;
        for (let i = 1; i < n; i++) {
            let x = output[i - 1]; let y = output[i]; let secondX = -1; let secondY = -1;
            if (x + width < n) { secondX = rank[x + width]; } if (y + width < n) { secondY = rank[y + width]; }
            next[y] = next[x]; if (rank[x] != rank[y] || secondX != secondY) { next[y]++; }
        }
        for (let i = 0; i < n; i++) { rank[i] = next[i]; }
        if (rank[output[n - 1]] == n - 1) { break; }
    }
    return 0;
}

Source

The 1993 journal exposition follows earlier conference work. The playground uses insertion sorting for rank pairs.