算法 5.1 三种字符串排序方法

//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););
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值