How to sort array suffixes in block sorting
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Block sorting, commonly recognized through the Burrows–Wheeler Transform (BWT), is a critical component in data compression and text processing. One of the key tasks in implementing block sorting techniques involves sorting the suffixes of an array. This article delves into the intricacies of sorting array suffixes in block sorting, suitable for enhancing understanding among software developers and computer scientists.
Suffix Array Overview
A suffix array is a sorted array of all suffixes of a string. It is deeply rooted in string processing algorithms, used for solving complex string-related problems more efficiently. In the context of block sorting, suffix arrays are pivotal as they facilitate the BWT's transformation, yielding better compression ratios by clustering similar characters.
Consider a string s = "banana". The suffixes of s are:
- "banana"
- "anana"
- "nana"
- "ana"
- "na"
- "a"
When sorted lexicographically, they appear as:
- "a"
- "ana"
- "anana"
- "banana"
- "na"
- "nana"
The suffix array contains the starting indices of these sorted suffixes, such as [5, 3, 1, 0, 4, 2].
Techniques to Sort Array Suffixes
Naive Approach
A straightforward approach involves generating all possible suffixes and sorting them using a comparison-based algorithm like QuickSort or MergeSort:
Though conceptually simple, this approach has an time complexity due to the overhead of sorting, making it impractical for large strings.
Efficient Algorithms
- Suffix Array with DC3 Algorithm (Kärkkäinen-Sanders-Burkhardt): The DC3 algorithm, also known as skew or the KSB algorithm, constructs the suffix array in time. The process involves:
- Generating triplets of suffix indices modulo 3.
- Sorting these triplets and recursively solving reduced problems.
- Merging the results.
- Manber-Myers Algorithm: This is an algorithm typically preferred for its simplicity over DC3. It iteratively doubles the span of considered substrings and employs a rank-based sorting:
Key Considerations and Trade-offs
| Approach | Time Complexity | Space Complexity | Remark |
| Naive | Inefficient for long strings but easy to implement. | ||
| DC3 Algorithm | Optimal but complex; suitable for very large inputs. | ||
| Manber-Myers Algorithm | Balanced approach with simpler implementation than DC3. |
Applications
Sorted suffix arrays greatly enhance text searching, pattern matching, and data compression techniques like BWT in tools such as bzip2. They help in detecting repeated substrings, approximating matching, and even in bioinformatics for genome alignment tasks.
Conclusion
Sorting suffix arrays is a profound and necessary step in block sorting algorithms. Efficient methods like the DC3 and Manber-Myers present viable solutions for varying constraints and requirements of modern applications. Understanding these techniques provides invaluable insights into the power of text processing and compression, propelling advancements in computational technologies.
Related reading
- How to sort by two fields in Java?
- How to sort Counter by value? - python
- How to sort depended objects by dependency
- How to sort faster than n log n given a strong condition on the list?
- How to sort List of objects by some property
- How to sort one list based on another?
- How to sort in-place using the merge sort algorithm?
- How to sort ListFile to list directories first and grouping files by directory?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.