Radix Sort Java Implementation

 1 int main()
 2 {
 3     int arr[] = {53,3,542,748,14,214};
 4 
 5 }
 6 void radixSort(int[] arr)
 7 {    
 8         //Buckets (one bucket = one array)
 9         int[][] bucket = new int[10][arr.length]
10         //In order to record the # of vals stored in each bucket each time,we use an array to hold the these 10 #. 
11         int[] bucketElementCounts = new int[10];
12 
13         //Iterations (LSN)
14         for(int i=0,n=1;i<N;i++,n*=10)
15         {
16             for(int j = 0; j < arr.length; j++)
17             {
18                 int digitOfElement = arr[j]/n%10;
19 
20                 //Put them into corrsponding bucket
21                 bucket[digitOfElement][bucketElementCounts[digitOfElement]] 
22                 =arr[j];
23                 bucketElementCounts[digitOfElement]++; 
24             }
25             //Traverse every bucket.Put back data into sequence.
26             int index = 0;
27             for(int k= 0; k < 10; k++  )
28             {
29                 if(bucketElementCounts[k] != 0)
30                 {
31                     for(int l=0; l<bucketElementCounts[k];l++)
32                     {
33                         arr[index] = bucket[k][l];
34                         index++;
35                     }
36                 }
37                 //important, bucketElementCounts[k] = 0
38                 bucketElementCounts[k] = 0;
39             }
40             PRINT: (arr)
41         }
42 }
原文地址:https://www.cnblogs.com/JasperZhao/p/12825233.html