1 packageahe.sort;2
3 importjava.io.BufferedReader;4 importjava.io.IOException;5 importjava.io.InputStreamReader;6 importjava.util.LinkedList;7
8 /**
9 * 基数排序算法10 *11 *@authorJohness12 *13 */
14 public classRadixSort {15
16 /**主队列*/
17 private LinkedListmainQueue;18 /**子队列*/
19 private LinkedList[] subQueues;20 /**子队列个数,作用不大*/
21 private final int SIZE = 10;22 /**当前容器(主队列)中存储数值的最大位数*/
23 private intmaxDigits;24
25 /**构造函数*/
26 publicRadixSort() {27 mainQueue = new LinkedList();28 subQueues = newLinkedList[SIZE];29 for(int i = 0; i < SIZE; ++i)30 subQueues[i] = new LinkedList();31 maxDigits = 0;32 }33
34 /**向容器中(主队列)添加一个数值*/
35 public voidadd(Integer num) {36 int digits =String.valueOf(num).length();37 if (digits >maxDigits)38 maxDigits =digits;39 mainQueue.add(num);40 }41
42 /**排序*/
43 public voidsort() {44 for (int i = 1; i <= maxDigits; ++i) {45 while (mainQueue.size() > 0) {46 Integer element =(Integer) mainQueue.pop();47 String elementTmpStr =String.valueOf(element);48 if (elementTmpStr.length()
56 for (int j = 0; j < SIZE; ++j) {57 mainQueue.addAll(subQueues[j]);58 subQueues[j].clear();59 }60 //listMainQueue();
61 }62 }63
64 /*==============================================================================================*/
65 //以下方法为测试方法(以下方法来自于Arizona State University学校课后作业,本人只做翻译,如该资源侵犯了您的权益,请及时联系我)66 //您可以访问67 // https://courses.eas.asu.edu/cse205/current/assignments/assignment11/Assignment11.java
68 // https://courses.eas.asu.edu/cse205/current/assignments/assignment11/Sorting.java
69 //查看本文参考内容70 //本文输入输出对照表可从该课后作业中获得71 //输入表72 // https://courses.eas.asu.edu/cse205/current/assignments/assignment11/input1.txt
73 // https://courses.eas.asu.edu/cse205/current/assignments/assignment11/input2.txt
74 // https://courses.eas.asu.edu/cse205/current/assignments/assignment11/input3.txt
75 // https://courses.eas.asu.edu/cse205/current/assignments/assignment11/input4.txt
76 //输出表77 // https://courses.eas.asu.edu/cse205/current/assignments/assignment11/output1.txt
78 // https://courses.eas.asu.edu/cse205/current/assignments/assignment11/output2.txt
79 // https://courses.eas.asu.edu/cse205/current/assignments/assignment11/output3.txt
80 // https://courses.eas.asu.edu/cse205/current/assignments/assignment11/output4.txt
81 /*==============================================================================================*/
82 /**
83 * 列举(输出)主队列数据84 */
85 public voidlistMainQueue() {86 System.out.println("mainQueue = " + listQueue(mainQueue) + "\n");87 }88
89 /**
90 * 列举(输出)子队列数据91 */
92 public voidlistSubQueues() {93 String result = "";94 for (int i = 0; i < SIZE; i++) {95 result += "subQueue[" + i + "]:";96 result +=listQueue(subQueues[i]);97 result += "\n";98 }99 System.out.println(result);100 }101
102 /**
103 * 列举某队列中数据项104 *105 * 方法使用了一个临时队列来完成数据轮循106 * 先从目标队列中逐个取出(采用取出并删除的方式)放入临时队列并做列举操作(连接到返回字符串)107 * 待轮循完成后再将临时队列中的数据存入回目标队列108 *109 *@paramqueue 目标队列110 *@return包含目标队列中所有数据的字符串111 */
112 private String listQueue(LinkedListqueue) {113 LinkedList temp = new LinkedList();114 String result = "{ ";115
116 while (!queue.isEmpty()) {117 Integer removed =queue.remove();118 result += removed + " ";119 temp.offer(removed);120 }121 result += "}\n";122
123 while (!temp.isEmpty()) {124 Integer removed2 =temp.remove();125 queue.offer(removed2);126 }127 returnresult;128 }129
130 public static voidmain(String[] args) {131 charinput1;132 String inputInfo = newString();133 String line = newString();134
135 RadixSort sort1 = newRadixSort();136
137 try{138 //打印菜单
139 printMenu();140
141 //创建流读取器读取用户输入
142 InputStreamReader isr = newInputStreamReader(System.in);143 BufferedReader stdin = newBufferedReader(isr);144
145 do{146 System.out.print("你想进行什么操作?\n");147 line = stdin.readLine().trim(); //读取一行
148 input1 = line.charAt(0);149 input1 =Character.toUpperCase(input1);150
151 if (line.length() == 1) //检查输入指令是否为单个152 //字符
153 {154 switch(input1) {155 case 'A': //添加一个数值
156 System.out.print("请输入要添加的数值:\n");157 inputInfo =stdin.readLine().trim();158 int num =Integer.parseInt(inputInfo);159 sort1.add(num);160 System.out.print("数值添加成功\n");161 break;162 case 'L': //列举数值
163 sort1.listMainQueue();164 break;165 case 'Q': //退出
166 break;167 case 'S': //排序
168 sort1.sort();169 System.out.print("排序完成\n");170 break;171 case '?': //显示帮助
172 printMenu();173 break;174 default:175 System.out.print("未知指令\n");176 break;177 }178 } else{179 System.out.print("未知指令\n");180 }181 } while (input1 != 'Q' || line.length() != 1);182 } catch(IOException exception) {183 System.out.print("IO Exception\n");184 }185 }186
187 /**打印控制台界面(菜单)*/
188 public static voidprintMenu() {189 System.out.print("选项\t\t动作\n" + "------\t\t------\n"
190 + "A\t\t添加一个数值\n" + "L\t\t列举队列\n"
191 + "Q\t\t退出\n" + "S\t\t排序数据\n"
192 + "?\t\t显示帮助\n\n");193 }194 }