package paixu;
import java.util.Arrays;
public class redixSort {
public static int max(int[] arr){
int maxs = -1;
for(int i=0;i<arr.length;i++){
if(maxs < arr[i]){
maxs = arr[i];
}
}
return maxs;
}
public static int getMaxDigit(int maxs){
int res = 0;
while(maxs > 0){
maxs = maxs/10;
res++;
}
return res;
}
public static void redix_Sort(int[] arr,int L,int R,int digit){
final int redix = 10;
int i = 0;
int j = 0;
int[] bucket = new int[R - L +1];
for(int d = 1;d <= digit ; d++){
int[] count = new int[redix];
for(i = L;i<=R;i++){
j = getDigit(arr[i],d);
count[j]++;
}
for(i = 1;i<redix;i++){
count[i] += count[i-1];
}
for(i = R;i>=L;i--){
j = getDigit(arr[i],d);
bucket[count[j]-1] = arr[i];
count[j] -=1;
}
for(i = L,j = 0;i<=R;i++,j++){
arr[i] = bucket[j];
}
System.out.println(Arrays.toString(arr));
}
}
public static int getDigit(int x,int d){
return ((x / (int)Math.pow(10, d-1))%10);
}
public static void main(String[] args) {
int[] arr = {12,87,65,94,56,23,85,22,54,63,189,256,378,1000,1006};
int max = max(arr);
int digit = getMaxDigit(max);
redix_Sort(arr,0,arr.length-1,digit);
}
}