//LSD
public class LSD {
public static void sort(String[] a, int w) {
int N = a.length;
int R = 256;
String[] aux = new String[N];
for (int i = w - 1; i >= 0; i--) {
int[] count = new int[R + 1];
for (int j = 0;j < N;j++) {
count[a[j].charAt(i) + 1]++;
}
for (int r = 0;r<R;r++) {
count[r + 1] += count[r];
}
for (int j = 0;j<N;j++) {
aux[count[a[j].charAt(i)]++] = a[j];
}
for (int j = 0;j<N;j++) {
a[j] = aux[j];
}
}
}
}
//MSD
import java.util.Map;
public class MSD {
private static int R = 256;
private static String[] aux;
private static int M = 10;
private static int charAt(String s, int d) {
return (d < s.length()) ? s.charAt(d) : -1;
}
public static void sort(String[] a) {
int N = a.length;
aux = new String[N];
sort(a, 0, N - 1, 0);
}
private static void sort(String[] a, int low, int high, int key) {
if (high <= low + M) {
InsertSort(a,low,high,key);
}
int[] count = new int[R + 2];
for (int i = low;i <= high;i++) {
count[charAt(a[i],key) +2]++;
}
for (int i = 1;i <= R; i++) {
count[i + 1] += count[i];
}
for (int i = low;i <= high;i++) {
aux[count[charAt(a[i],key) +1]++] = a[i];
}
for (int i = low,;i <= high;i++) {
a[i] = aux[i - low];//因为递归的缘故,a可能只是其中的一小段
}
for (int r = 0;r< R;r++) {
sort(a, low + count[r], low + count[r + 1] - 1, key + 1);
}
}
private static void InsertSort(String[] a, int low, int high, int d) {
for (int i = low;i <= high;i++) {
for (int j = i ;j > low && less(a[j],a[j - 1],d);j--) {
exch(a, j, j - 1);
}
}
}
private static boolean less(String a, String b, int d) {
return a.substring(d).compareTo(b.substring(d)) < 0;
}
}
//三项字符串快排
public class Quick3string {
private static int charAt(String s, int d) {
return (d < s.length()) ? s.charAt(d) : -1;
}
private static void sort(String[] a) {
sort(a, 0, a.length - 1, 0);
}
private static void sort(String[] a, int low, int high, int d) {
if (high <= low ) return;
int lt = low, gt = high;
int v = charAt(a[low], d);
int i = low + 1;
while (i <= gt) {
int t = charAt(a[i], d);
if (t < v) exch(a, lt++, i++);
else if(t > v) exch(a, gt--, i);
else i++;
}
sort(a, low, lt - 1, d);
if (v >= 0) sort(a, lt, gt, d);
sort((a), gt + 1, high, d););
}
}