private static void Sort(string[] a, int lo, int hi, int d) { //以d个字符为键将a[lo]至a[hi]排序 if (hi <= lo + M) { SortExample.InsertionSort(ref a, lo, hi); return; } int[] count = new int[R + 2]; for (int i = lo; i <= hi; i++) //计算频率 { count[CharAt(a[i], d) + 2]++; } for (int r = 0; r < R + 1; r++) //频率转换为索引 { count[r + 1] += count[r]; } for (int i = lo; i <= hi; i++)//数据分类 { aux[count[CharAt(a[i], d) + 1]++] = a[i]; } for (int i = lo; i < hi; i++) { a[i] = aux[i - lo]; } //递归的以每个字符为键进行排序 for (int i = 0; i < R; i++) { Sort(a, lo + count[i], lo + count[i + 1] - 1, d + 1); } }
public SuffixArray(string s) { N = s.Length; suffixes = new string[N]; for (int i = 0; i < N; i++) { suffixes[i] = s.Substring(i); } SortExample.ImprovedThreeWayQuickSort(ref suffixes); }