Skip to content

Commit bcddcfc

Browse files
Extracted sorting logic to a private method
1 parent 4826646 commit bcddcfc

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

src/main/java/com/thealgorithms/strings/SuffixArray.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,7 @@ public static int[] buildSuffixArray(String text) {
3030
for (int k = 1; k < n; k *= 2) {
3131
final int step = k;
3232

33-
// Comparator: first by rank, then by rank + step
34-
Arrays.sort(suffixArray, (a, b) -> {
35-
if (rank[a] != rank[b]) {
36-
return Integer.compare(rank[a], rank[b]);
37-
}
38-
int ra = (a + step < n) ? rank[a + step] : -1;
39-
int rb = (b + step < n) ? rank[b + step] : -1;
40-
return Integer.compare(ra, rb);
41-
});
33+
sortSuffixArray(suffixArray, rank, step, n);
4234

4335
// Re-rank
4436
tempRank[suffixArray[0]] = 0;
@@ -57,4 +49,18 @@ public static int[] buildSuffixArray(String text) {
5749
}
5850
return Arrays.stream(suffixArray).mapToInt(Integer::intValue).toArray();
5951
}
52+
53+
private static Integer[] sortSuffixArray(Integer[] suffixArray, int[] rank, int step, int n) {
54+
// Comparator: first by rank, then by rank + step
55+
Arrays.sort(suffixArray, (a, b) -> {
56+
if (rank[a] != rank[b]) {
57+
return Integer.compare(rank[a], rank[b]);
58+
}
59+
int ra = (a + step < n) ? rank[a + step] : -1;
60+
int rb = (b + step < n) ? rank[b + step] : -1;
61+
return Integer.compare(ra, rb);
62+
63+
});
64+
return suffixArray;
65+
}
6066
}

0 commit comments

Comments
 (0)