Java实现基数排序
时间: 2023-02-15 09:02:04 浏览: 92
的代码是什么?Java实现的基数排序代码如下:
```
import java.util.Arrays;
public class RadixSort {
public static void main(String[] args) {
int[] arr = { 4725, 4586, 1330, 8792, 1594, 5729 };
int max = Arrays.stream(arr).max().getAsInt();
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSort(arr, exp);
}
System.out.println(Arrays.toString(arr));
}
private static void countingSort(int[] arr, int exp) {
int[] output = new int[arr.length];
int[] count = new int[10];
Arrays.fill(count, 0);
for (int i = 0; i < arr.length; i++) {
count[(arr[i] / exp) % 10]++;
}
for (int i = 1; i < count.length; i++) {
count[i] += count[i - 1];
}
for (int i = arr.length - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (int i = 0; i < arr.length; i++) {
arr[i] = output[i];
}
}
}
```
阅读全文