《算法》-字符串[字符串排序]

2020-09-14  本文已影响0人  算法手记

引入

c)

标准字符表

在这里插入图片描述

键索引计数

输入字符串和字符串对应的组别(组别也是字符串的键)
在满足组别有小到大排序的情况下,将字符串按字母顺序排序
[图片上传失败...(image-24066d-1600017488526)]

算法步骤

第一步,记录组别的频率
(为了得到某个字符串在排序后的范围,比如组别2肯定在组别1后面,在组别3前面,把每个组别有多少个人记录下来,方便我们定位)

第三步,分类

第四步,复制

KeyIndexedCounting 代码

int N = a.length;
String[] aux = new String[N]; //访问数组N次
int[] count = new int[R+1]; //访问数组R+1次
// Compute frequency counts.
for(int i = 0;i<N;i++) //访问数组2N次
    count[a[i].key()+1]++;
// Transform counts to indices.
for(int r = 0;r<R;r++) //访问数组2R次,进行R次加法
    count[r+1]+=count[r];
// Distribute the records.
for(int i = 0;i<N;i++) //访问数组3N次,使计数器值增大N次并移动数据N次
    aux[count[a[i].key()]++]=a[i];
// Copy back.
for(int i = 0;i<N;i++) //访问数组2N次,移动数据N次
    a[i]=aux[i];

低位优先排序

结合索引排序,从字符串的低位(从右面开始),从右到左,每个字符都当一次该字符串的键,给整个字符串排序

以下代码的局限性:每个字符串的长度是相等的。稍作修改可适应不等长的字符串。

LSD 代码

public class LSD {
    public static void sort(String[] a, int W) { // Sort a[] on leading W characters.
        int N = a.length;
        int R = 256;
        String[] aux = new String[N];
        for (int d = W - 1; d >= 0; d--) { // Sort by key-indexed counting on dth char.
            int[] count = new int[R + 1];  // 创建数组大小为R+1
            for (int i = 0; i < N; i++) // Compute frequency counts. 频率
                count[a[i].charAt(d) + 1]++;
            for (int r = 0; r < R; r++) // Transform counts to indices. 索引
                count[r + 1] += count[r];
            for (int i = 0; i < N; i++) // Distribute. 按组别丢到副本里去
                aux[count[a[i].charAt(d)]++] = a[i];
            for (int i = 0; i < N; i++) // Copy back. 赋回正本
                a[i] = aux[i];
        }
    }
}

高位优先排序

考虑不等长字符串的比较

MSD 代码

public class MSD {
    private static int R = 256; // radix 256个字符
    private static final int M = 15; // cutoff for small subarrays 数组小到多少的时候用插入排序?
    private static String[] aux; // auxiliary array for distribution 副本
 
    private static int charAt(String s, int d) {
        if (d < s.length())
            return s.charAt(d);
        else
            return -1;
    }
 
    public static void sort(String[] a) {
        int N = a.length;
        aux = new String[N];
        sort(a, 0, N - 1, 0);
    }
 
    // Sort from a[lo] to a[hi], starting at the dth character.
    private static void sort(String[] a, int lo, int hi, int d) { 
        //如果数组较小,插入排序,具体实现略
        if (hi <= lo + M) {
            Insertion.sort(a, lo, hi, d);
            return;
        }
        
        int[] count = new int[R + 2]; // 数组大小R+2
        for (int i = lo; i <= hi; i++)// Compute frequency counts.频次,只累计了hi-lo+1次
            count[charAt(a[i], d) + 2]++; // 每个对应数字在原来基础上+1
        for (int r = 0; r < R + 1; r++) // Transform counts to indices. 索引
            count[r + 1] += count[r];
        for (int i = lo; i <= hi; i++) // Distribute.丢到对应组别里去
            aux[count[charAt(a[i], d) + 1]++] = a[i]; // 每个对应数字在原来基础上+1
                                                      // aux的赋值从aux[0]开始,到aux[hi-lo]结束
                                                      // 在这里count会发生变化。原来这里的count只是为了移动到下一位为下一个元素找位置用,现在这里的count[i]还可以通过是否到达count[i+1]来判断是否可以结束递归
        for (int i = lo; i <= hi; i++) // Copy back. 注意aux的起终点和a的对应关系
            a[i] = aux[i - lo];
        // Recursively sort for each character value.
        for (int r = 0; r < R; r++) //私认为初始化条件r=1更好,因为r=0都是字符为空的子字符串
            sort(a, lo + count[r], lo + count[r + 1] - 1, d + 1); // 将当前相同字符的分为一组,每组以下一位字符为比较对象排序
    }
}

三向字符串快速排序

Quick3string 代码

public class Quick3string {
    private static int charAt(String s, int d) {
        if (d < s.length())
            return s.charAt(d);
        else
            return -1;
    }
 
    public static void sort(String[] a) {
        sort(a, 0, a.length - 1, 0);
    }
 
    private static void sort(String[] a, int lo, int hi, int d) {
        if (hi <= lo)
            return;
        int lt = lo, gt = hi; // 低位指针,高位指针
        int v = charAt(a[lo], d); // 切分值
        int i = lo + 1; // 从第二个字符串的d位开始
        while (i <= gt) {
            int t = charAt(a[i], d);
            if (t < v) // 比切分值小,放到切分值前面去
                exch(a, lt++, i++);
            else if (t > v) // 比切分值大,放到最后去
                exch(a, i, gt--);
            else
                i++;
        }
 
        // a[lo..lt-1] < v = a[lt..gt] < a[gt+1..hi]
        sort(a, lo, lt - 1, d);
        if (v >= 0) // d位字母相同且不为空,则这部分从下一位开始再比较
            sort(a, lt, gt, d + 1);
        sort(a, gt + 1, hi, d);
    }
}
上一篇下一篇

猜你喜欢

热点阅读