基数排序
import java.util.Arrays;
public class RadixSort {
public static void main(String[] args) {
int [] arr= {53,3,542,748,14,214};
RADIXSort(arr);
}
public static void radixSort(int []arr){
int [][] bucket = new int[10][arr.length];
int [] bucketElementCounts =new int[10];
for(int j=0;j<arr.length;j++){
int digitOfElement = arr[j]%10;
bucket[digitOfElement][bucketElementCounts[digitOfElement]] = arr[j];
bucketElementCounts[digitOfElement]++;
}
int index =0;
for (int k = 0; k < bucketElementCounts.length; k++) {
if (bucketElementCounts[k] != 0) {
for (int l = 0; l < bucketElementCounts[k]; l++) {
arr[index]= bucket[k][l];
index++;
}
}
bucketElementCounts[k]=0;
}
System.out.println("第一轮的排序是"+Arrays.toString(arr));
for(int j=0;j<arr.length;j++){
int digitOfElement = arr[j]/10%10;
bucket[digitOfElement][bucketElementCounts[digitOfElement]] = arr[j];
bucketElementCounts[digitOfElement]++;
}
index =0;
for (int k = 0; k < bucketElementCounts.length; k++) {
if (bucketElementCounts[k] != 0) {
for (int l = 0; l < bucketElementCounts[k]; l++) {
arr[index]= bucket[k][l];
index++;
}
}
bucketElementCounts[k]=0;
}
System.out.println("第二轮的排序是"+Arrays.toString(arr));
for(int j=0;j<arr.length;j++){
int digitOfElement = arr[j]/100%10;
bucket[digitOfElement][bucketElementCounts[digitOfElement]] = arr[j];
bucketElementCounts[digitOfElement]++;
}
index =0;
for (int k = 0; k < bucketElementCounts.length; k++) {
if (bucketElementCounts[k] != 0) {
for (int l = 0; l < bucketElementCounts[k]; l++) {
arr[index]= bucket[k][l];
index++;
}
}
}
System.out.println("第三轮的排序是"+Arrays.toString(arr));
}
public static void RADIXSort(int[]arr){
int max = arr[0] ;
int index ;
for(int i =1;i<arr.length;i++){
if(arr[i] > max){
max= arr[i];
}
}
System.out.println(max);
int maxLength = (max+"").length();
int[][] bucket =new int [10][arr.length];
int [] bucketElementCounts = new int[10];
for(int i=0,n=1; i<maxLength;i++, n*=10){
for(int j=0; j<arr.length; j++){
int digitOfElement = arr[j]/n%10;
bucket[digitOfElement][bucketElementCounts[digitOfElement]]=arr[j];
bucketElementCounts[digitOfElement]++;
}
index =0;
for(int k=0 ; k<bucketElementCounts.length;k++){
if(bucketElementCounts[k]!=0){
for(int l =0; l<bucketElementCounts[k];l++){
arr[index]=bucket[k][l];
index++;
}
}
bucketElementCounts[k] = 0;
}
System.out.println("****************");
System.out.println("第"+(i+1)+"轮的排序结果为"+Arrays.toString(arr));
}
}
}