字符串排序

1、低位优先字符串排除LSD 来自《算法第四版》算法5.1 --------------P459

/**
 *@author ShaoChenrong
 * 
 */
/**低位优先字符串排序LSD*/
public class LSD
{

	public static void sort(String[] a, int w)
	{
		int R = 256;
		int N = a.length;
		String[] aux = new String[N];
		for (int d = w-1; d >= 0; d--)
		{
			int[] count = new int[R+1];
			for (int i = 0; i < N; i++)
				count[a[i].charAt(d) + 1]++;
			for (int i = 0; i < R; i++)
				count[i + 1] += count[i];
			for (int i = 0; i < N; i++)
				aux[count[a[i].charAt(d)]++] = a[i];
			for (int i = 0; i < N; i++)
				a[i] = aux[i];
		}
	}
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		String[] a = {"4PGC938","2IYE230","3CIO720","1ICK750","10HV845","4JZY524","1ICK750","3CIO720"};
		sort(a,7);
		for (int i = 0; i < a.length; i++)
			System.out.println(a[i]);

	}

}

2、高位优先字符串排序MSD 来自《算法第四版》算法5.2 --------------P462

public class MSD
{
	private static int R = 256;
	private static String[] aux;
	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);
	}
	private static void sort(String[] a, int lo, int hi, int d)
	{
		if (hi <= lo) 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 r = 0; r < R; r++)
			sort(a, lo + count[r], lo + count[r + 1] - 1, d + 1);
	}
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		String[] a = {"she","shells","abc","boy","great"};
		//MSD msd = new MSD();
		sort(a);
		for (int i = 0; i < a.length; i++)
		System.out.println(a[i]);
	}

}
小贴士:
由于算法5.2忘记写前面if循环,导致java.lang.StackOverflowError错误,即当前线程堆栈满了,函数调用层级过多导致,例如死递归。

3、三向字符串快速排序 来自《算法第四版》算法5.3 --------------P469

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)
	{
		//int N = a.length;
		//aux = new String[N];
		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;
		int gt = hi;
		int v = charAt(a[lo], d);
		int i = lo + 1;
		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++;
		}
		sort(a, lo, lt - 1, d);
		if (v >= 0) sort(a, lt, gt, d + 1);
		sort(a, gt + 1, hi, d);
		
	}
	private static void exch(String[] a, int i, int j)
	{
		String temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		String[] a = {"she","shells","abc","boy","great"};
		//MSD msd = new MSD();
		sort(a);
		for (int i = 0; i < a.length; i++)
		System.out.println(a[i]);
	}

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值