首页 > 编程语言 > 详细

Radix Sort - Java Implementation

时间:2020-05-04 10:21:16      阅读:76      评论:0      收藏:0      [点我收藏+]
 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 }

 

Radix Sort - Java Implementation

原文:https://www.cnblogs.com/JasperZhao/p/12825233.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!