字符串排序--低位优先法

参考:算法第四版

package com.lsd;

public class LSD {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] str = {"ab0","Hc2","cs3","ab0","sdg"};
		sort(str, 3);
	}
	
	public static void sort(String[] a,int w) {
		
		int N = a.length;
		int R = 256;
		int[] count = new int[R+1];
		String[] aux = new String[N];
		//统计各数出现的频率
		for (int i = 0; i < N; i++) 
			count[a[i].charAt(w-3)+1]++;
		//将频率转换成索引
		for (int r = 0; r < R; r++) 
			count[r+1] = count[r+1] + count[r];
		//将元素分类
		for (int i = 0; i < N; i++) 
			aux[count[a[i].charAt(w-3)]++] = a[i];
		//将元素输出
		for (int i = 0; i < N; i++)
		System.out.println(aux[i]);
	}

}


你可能感兴趣的:(菜鸟算法学习之旅)