Program work 16. Radix Sort in Java
来源:互联网 发布:snap.svg.js 编辑:程序博客网 时间:2024/05/22 00:44
不是基于比较的排序
是基于位数上的排序, 且仅适用于整数.
此位数的排序不会影响前一位数排序好的相对位置, 是stable的排序
影响效能的关键在于数组中最大数字的位数
最差时间复杂度为O(d*n).
另外需要O(n)space
d是最大数字的位数, 决定循环的轮数, n是每轮处理的数目的大小.
Pseudocode
radixSort (arr, tmp){ get the max of the array d ←the digit number of max for (i ← digit; i > 0; i--) { resetCounter for each same digit position { counter[pos]++; } combineCounter; for j ← end; j >= 0; j-- { put the arr[j] to the tmp, pos is determined by the counter; counter[pos]--; }}
Java
package ncku.cdc.sorting;import java.util.Random;public class RadixSort { private int[] sequence; private int[] counter; public RadixSort(int size, int range) { sequence = new int[size]; counter = new int[10]; Random rand = new Random(); for (int i = 0; i < size; i++) { sequence[i] = rand.nextInt(range); } } public static void main(String[] args) { int size = Integer.valueOf(args[0]); int range = Integer.valueOf(args[1]); RadixSort radix = new RadixSort(size, range); System.out.println("before radixSort:"); SortingTools.validation(radix.getSequence(), 0); int[] tmp = new int [size]; radix.radixSort(radix.getSequence(), tmp, size); System.out.println("after radixSort:"); SortingTools.validation(radix.getSequence(), 0); } public void radixSort(int[] arr, int[] tmp, int len) { int digit = getDigit(arr, len); int radix = 1; for (int d = digit; d > 0; d--) { resetCounter(); int index; for (int i = 0; i < len; i++) { index = (arr[i] / radix) % 10; counter[index]++; } caculateCounter(); for (int j = len - 1; j >= 0; j--) { index = (arr[j] / radix) % 10; tmp[--counter[index]] = arr[j]; } radix *= 10; System.arraycopy(tmp, 0, arr, 0, len); } } private int getDigit(int[] arr, int len) { int max = Integer.MIN_VALUE; for (int i = 0; i < len; i++) { if (arr[i] > max) { max = arr[i]; } } int d = 1; while ((max /= 10) != 0) { d++; } return d; } private void resetCounter() { for (int i = 0; i < 10; i++) { counter[i] = 0; } } private void caculateCounter() { for (int i = 1; i < 10; i++) { counter[i] += counter[i - 1]; } } public int[] getSequence() { return sequence; }}
程序输入: 35 200. 表示生成一个长度35, 数字范围为[0, 200)的数组
程序输出:
before radixSort:
0 67 2 173 160 57 36 16 39 76 141 103 128 195 23 71 43 47 160 114 186 6 17 36 158 74 79 133 99 160 28 113 108 171 171
after radixSort:
0 2 6 16 17 23 28 36 36 39 43 47 57 67 71 74 76 79 99 103 108 113 114 128 133 141 158 160 160 160 171 171 173 186 195
0 0
- Program work 16. Radix Sort in Java
- Program work 11. Quick Sort in Java
- Program work 12. Merge Sort in Java
- Program work 13. Heap Sort in Java
- Program work 14. Insertion Sort in Java
- Program work 15. Shell Sort in Java
- Program work 17. Selection Sort in Java
- Program work 18. Bubble Sort in Java
- Radix sort
- Radix Sort
- radix sort
- Radix Sort
- radix-sort
- radix sort
- radix sort
- Radix Sort
- radix sort &&bucket sort
- path in java program
- 整月(月末)增加固定的几个月后所得到的日期
- 自定义带文字的返回按钮
- 双击消息(OnNMDblclkList)处理函数中不能设断点的问题
- mac重置root密码
- Excel中的Data Validation(控制输入值从下拉框选)
- Program work 16. Radix Sort in Java
- 币种查询
- OpenCV仿射变换+投射变换+单应性矩阵
- 启动startWebLogic.cmd时提示:此时不应有\java\jdk1.7.0_17\lib
- Sql server 2008 的完成备份和差异备份
- linux查找文件find命令
- JS时间大全 Date (对象)
- 博客开篇
- delphi类与对象的转化