()
| 50 | } |
| 51 | |
| 52 | public static void sort() { |
| 53 | // 如果会溢出,那么要改用long类型数组来排序 |
| 54 | // 找到数组中的最小值 |
| 55 | int min = arr[0]; |
| 56 | for (int i = 1; i < n; i++) { |
| 57 | min = Math.min(min, arr[i]); |
| 58 | } |
| 59 | int max = 0; |
| 60 | for (int i = 0; i < n; i++) { |
| 61 | // 数组中的每个数字,减去数组中的最小值,就把arr转成了非负数组 |
| 62 | arr[i] -= min; |
| 63 | // 记录数组中的最大值 |
| 64 | max = Math.max(max, arr[i]); |
| 65 | } |
| 66 | // 根据最大值在BASE进制下的位数,决定基数排序做多少轮 |
| 67 | radixSort(bits(max)); |
| 68 | // 数组中所有数都减去了最小值,所以最后不要忘了还原 |
| 69 | for (int i = 0; i < n; i++) { |
| 70 | arr[i] += min; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // 返回number在BASE进制下有几位 |
| 75 | public static int bits(int number) { |
no test coverage detected