题目:
利用队列实现对某一个数据序列的排序(采用基数排序),其中对数据序列的数据(第1和第2条进行说明)和队列的存储方式(第3条进行说明)有如下的要求:
1)当数据序列是整数类型的数据的时候,数据序列中每个数据的位数不要求等宽,比 如:
1、21、12、322、44、123、2312、765、56
2)当数据序列是字符串类型的数据的时候,数据序列中每个字符串都是等宽的,比 如:
"abc","bde","fad","abd","bef","fdd","abe"
3)要求重新构建队列的存储表示方法:使其能够将 n 个队列顺序映射到一个数组 listArray 中,每个队列都表示成内存中的一个循环队列【这一项是可选项】
基数排序思想:
以整数为例:先建立十个桶,编号为0-9。然后检测要排序的整数,
从各位数字开始,到数据中存在的最高的位数。根据每一位数字的值把所有要排序的整数放到对应数字的桶中。
比如,整数73 22 93 43 55 14 28 65 39 81。
第一趟:
第二趟:
如果要排序的数字中有更高的位数,必须多次排序。但是这样就比较花时间。所以对于位数相差较大的一串数字的排序,不建议采用基数排序。基数排序的时间复杂度为O(n)。空间复杂度为O(n+bucket)。bucket为桶的数目。故空间复杂度约等于O(n)。当每个桶之后的数字已知的时候,可以把储存基数排序的空间结构变成简单的一维数组。
队列:先进先出特性。push和pop分别入队出队。
具体实现代码:
1 //Programming in Java
2 import java.util.*;3
4 public classradixSort {5
6 private static final HashMap alphaToNum = newHashMap();7
8 static{9 alphaToNum.clear();10 initAlpha2Num('A', 'Z', alphaToNum);11 initAlpha2Num('a', 'z', alphaToNum);12
13 }14
15 private static final void initAlphaToNum(char c1, char c2, HashMapmap) {16 for (char c = c1; c <= c2; c++)17 map.put(c, map.size());18 }19
20 public static voidmain(String[] args) {21 radixSort sortInstance = newradixSort();22 int[] data = { 1, 4, 3, 3, 21, 12, 322, 44, 123, 2312, 765, 56, 8978, 10000, 14, 28, 65, 39, 81, 33, 100, 567};23 String[] data2 = { "abc", "Bde", "fad", "abd", "Bef", "fdd", "abe"};24 sortInstance.sort(data);25 sortInstance.sort(data2);26
27 print(data);28 print(data2);29 }30
31 public static voidprint(String[] data) {32 for (int i = 0; i < data.length; i++)33 System.out.print(data[i] + " ");34 System.out.println();35 }36
37 public static void print(int[] data) {38 for (int i = 0; i < data.length; i++)39 System.out.print(data[i] + " ");40 System.out.println();41 }42
43 public void sort(int[] a) {44 int N = 10;45 ArrayList>qArray;46 qArray = newArrayList();47 for (int i = 0; i < N; i++)48 qArray.add(new LinkedList());//开辟空间
49 int max =Integer.MIN_VALUE;50 for (int i = 0; i < a.length; i++)51 max =Integer.max(max, a[i]);52 int[] length = new int[N];//记录qArray的原始长度,防止对元素重复操作53 //radix sort
54 for (int i = 0; i < a.length; i++)55 qArray.get(a[i] % 10).offer(a[i]);//1 step
56 for (int j = 10; j <= max; j *= 10) {57 for (int i = 0; i < N; i++)58 length[i] = qArray.get(i).size(); //2 step update length
59 for (int i = 0; i < N; i++) {60 Queue q =qArray.get(i);61 while (length[i]-- > 0) {62 int num =q.poll();63 qArray.get((num / j) % 10).offer(num);64 }65 }66 }67 for (int i = 0, AIndex = 0; i < N; i++) //finally
68 while (qArray.get(i).size() > 0)69 a[AIndex++] =qArray.get(i).poll();70 }71
72 public voidsort(String[] s) {73 int N =alphaToNum.size();74 ArrayList>qArray;75 qArray = newArrayList();76 for (int i = 0; i < N; i++)77 qArray.add(new LinkedList());//开辟空间
78 int strLen = s[0].length();79 int[] length = new int[N];//forbid duplicate operation for (int i = 0; i < s.length; i++)
80 qArray.get(alphaIndex(s[i].charAt(s[i].length() - 1))).offer(s[i]);81 for (int j = strLen - 1; j > 0; j--) {82 for (int i = 0; i < N; i++)83 length[i] =qArray.get(i).size();84 for (int i = 0; i < N; i++) {85 Queue q =qArray.get(i);86 while (length[i]-- > 0) {87 String str =q.poll();88 qArray.get(alphaIndex(str.charAt(j - 1))).offer(str);89 }90 }91 }92 for (int i = 0, AIndex = 0; i < N; i++) //finally
93 while (qArray.get(i).size() > 0)94 s[AIndex++] =qArray.get(i).poll();95 }96
97 private int alphaIndex(charc) {98 returnalphaToNum.get(c);99 }100 }
ps:阿爸的代码