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.
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.
Step 0: Initial state.
State at this checkpoint
{ "values": [ 0, 1, 2, 3, 4, 5 ], "active": [] }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" ] }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" ] }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" ] }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:
- 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 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.
- Suffix Arrays: A New Method for On-Line String Searches
Udi Manber; Gene Myers (1993). Research paper.