Suffix Array Explained: Searching Text at Scale

Suffix array explained in plain terms means understanding how sorting the endings of a text turns searching into a quick binary search. The central question is how a program can find every occurrence of a word in a huge document without scanning it from the start, and the answer is to sort all the suffixes once and then let binary search do the rest.

A suffix array is a sorted list of the starting positions of every suffix of a text, so that all occurrences of any pattern sit together in one continuous block. Searching for a pattern becomes a binary search over the sorted suffixes, which runs in logarithmic time plus the length of the pattern, however large the text grows.

The idea is a quiet classic of string algorithms: pay for one sort up front, and every search afterwards is cheap. This article explains what a suffix array is, how it is built and searched, and where it is used.

Key takeaways

  • A suffix array lists every suffix of a text in sorted order.
  • All occurrences of a pattern sit together in one block.
  • Searching is a binary search, logarithmic in the text length.
  • It uses far less memory than a suffix tree for the same text.
  • It powers text search, genomics, and compression tools.

What a suffix array is

Take a short word like banana. Its suffixes are banana, anana, nana, ana, na, and a, and sorting them gives a, ana, anana, banana, na, nana. The suffix array is simply the list of starting positions in that sorted order: five, three, one, zero, four, two. Nothing else is stored, just the order, and that order is enough to make searching fast.

The key property is that suffixes sharing a prefix sit next to each other in the sorted order. Every occurrence of the pattern ana begins a suffix starting with ana, and all such suffixes form one continuous block in the array, so finding the pattern means finding the edges of that block. Binary search finds those edges by comparing the pattern with the suffix in the middle, then narrowing to the correct half.

The structure is deliberately minimal. A suffix tree stores the same information with far more pointers and nodes, while a suffix array stores only integers, one per character, which makes it compact enough to hold in memory for texts that a suffix tree could never fit. Our guide to inverted indexes explains a different text index that maps words to documents instead of suffixes to positions.

How a suffix array is built and searched

Building a suffix array naively means sorting all the suffixes, and comparing two suffixes can cost as much as the text is long, which makes the naive build slow for large texts. The practical algorithms are cleverer: they sort by the first character, then by the first two, then by the first four, doubling the prefix length each round and reusing the previous round’s ranks to compare in constant time. The doubling method builds the array in linearithmic time.

Modern construction algorithms go further and build the array in linear time, but the doubling method is the one most programmers implement first, because it is short, correct, and fast enough for texts of millions of characters. The important thing is that construction happens once, while searches happen many times, so a slightly slower build is a fine price for fast queries.

Searching uses binary search with a twist. Comparing the pattern with a suffix costs as much as the pattern is long, so a search costs the pattern length times the logarithm of the text length. The longest-common-prefix array, usually built alongside the suffix array, cuts that cost down by remembering how much of each comparison was already known, and it also unlocks a second use: finding repeated substrings and the longest repeated pattern in the text.

Finding all occurrences, not just one, means finding the lower and upper edges of the matching block. Two binary searches locate the edges, and every position between them is an occurrence, so reporting a hundred matches costs no more than reporting one, apart from listing them. That is the property that makes the suffix array a genuine index rather than a party trick.

A photograph of an old library index room with long rows of labelled card drawers stretching into the distance
A photograph of an old library index room with long rows of labelled card drawers stretching into the distance, shows how a suffix array sorts every ending of a text so any pattern can be found fast.

Suffix array versus other text indexes

A suffix array is one of several ways to index text, and each suits a different shape of problem. A suffix tree answers the same queries faster but uses far more memory, a hash table of n-grams answers exact word lookups in constant time but cannot find substrings, and an inverted index maps words to documents rather than positions in one text.

The table below sets out the main contrasts between the leading approaches.

Aspect Suffix array Suffix tree Inverted index
Pattern search time Logarithmic times pattern length Linear in pattern length Constant per word
Memory used One integer per character Many pointers per character One entry per distinct word
Substring search Yes, any substring Yes, any substring Words only, not substrings
Repeated patterns Yes, with the prefix array Yes, directly Not supported
Best for Large texts, modest memory Fastest substring queries Search engines over documents

The practical result is that a suffix array is the right default when memory matters and the text is large, while a suffix tree wins when raw query speed matters more than the memory bill.

Where suffix arrays are used

Genomics is the suffix array’s most famous home. A genome is a text three billion letters long, and finding where a short sequence occurs in it is a substring search, which is exactly what a suffix array answers. Modern aligners use compressed variants of the same idea to map millions of DNA reads against a reference genome every day. Our guide to Elasticsearch explains how search engines index text at a different scale, across documents rather than within one.

Text editors and code search tools use suffix arrays to find every occurrence of a string in a large file or codebase. A search across a million lines of code is a substring problem, and an index over suffixes answers it without scanning every line, which is why the response feels instant even on enormous repositories.

Compression tools use suffix arrays to find repeated substrings, because the longest match for a piece of text is found among its neighbours in the sorted suffix order. The Burrows-Wheeler transform, at the heart of the bzip2 compressor, is closely related to the suffix array, and both exploit the same sorted structure of a text’s endings.

Plagiarism detection and document comparison use suffix arrays to find long shared passages between texts. Two documents that share a paragraph share a long substring, and the suffix array of their concatenation puts those shared suffixes side by side, so the longest common passage falls out of the same structure that powers search.

Strengths and limits

The greatest strength of a suffix array is its economy. One integer per character buys logarithmic substring search, repeated-pattern detection, and longest-common-substring queries, all from a single sorted list. It is hard to name another structure that delivers so much from so little.

It is also a strength that the structure is static and simple. There are no pointers to maintain, no balance to restore, and no updates to handle, which makes it easy to store on disk, share between processes, and compress. Our guide to semantic versus keyword search shows how modern retrieval builds on these classic indexes rather than replacing them.

The limits are real, though. A suffix array is built for a fixed text, so editing the text means rebuilding the index, and a search costs the pattern length times a logarithm rather than the pattern length alone. For a text that changes constantly, an inverted index or a hash-based structure will serve better.

Suffix array checklist

  • Use a suffix array when the text is large and mostly fixed.
  • Build it once with the doubling method and reuse it for many searches.
  • Find both edges of the matching block to list all occurrences.
  • Build the prefix array alongside it for repeated-pattern queries.
  • Prefer a suffix tree when raw query speed beats memory concerns.
  • Prefer an inverted index for word search across many documents.
  • Remember the array stores positions, not the suffixes themselves.
  • Test searches on patterns at the start and end of the text.
  • Consider a compressed variant for genome-scale texts.

Frequently asked questions

What is a suffix array in simple terms?

It is a sorted list of the starting positions of every suffix of a text, so that all occurrences of any pattern sit together in one block.

How is a suffix array different from a suffix tree?

A suffix tree stores the same information as a tree of nodes and pointers and searches faster, while a suffix array stores only integers and uses far less memory.

How fast is a suffix array search?

Logarithmic in the text length times the pattern length, because it is a binary search over the sorted suffixes.

What is the longest-common-prefix array?

It is an array built alongside the suffix array that records how much neighbouring suffixes share, which speeds up searches and finds repeated patterns.

Can a suffix array find repeated substrings?

Yes, the longest repeated substring appears between neighbouring suffixes in the sorted order, which the prefix array reveals directly.

When should I not use a suffix array?

When the text changes often, because every edit means rebuilding the index, and a hash-based or inverted structure will serve better.

The bottom line

Suffix array explained in one line is this: sort every ending of the text once, and any substring can be found with a binary search.

It is the compact classic of text indexing, and the reason a three-billion-letter genome can be searched in milliseconds.

This article is general information about how suffix arrays work, not advice about choosing a data structure for a particular system.

Latest articles

Scroll to Top