[Java] java排序之-基数排序 →→→→→进入此内容的聊天室

来自 , 2020-03-23, 写在 Java, 查看 111 次.
URL http://www.code666.cn/view/18d10dc6
  1. import java.util.ArrayList;
  2.  
  3. import java.util.List;
  4.  
  5.  //(1)基本思想:将所有待比较数值(正整数)统一为同样的数位长度,数位较短的数前面补零。然后,从最低位开始,依次进行一次排序。这样从最低位排序  //一直到最高位排序完成以后,数列就变成一个有序序列。
  6.  
  7. public class radixSort {
  8.  
  9.          int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,101,56,17,18,23,34,15,35,25,53,51};
  10.  
  11. public radixSort(){
  12.  
  13.          sort(a);
  14.  
  15.          for(int i=0;i<a.length;i++)
  16.  
  17.                    System.out.println(a[i]);
  18.  
  19. }
  20.  
  21. public  void sort(int[] array){  
  22.  
  23.                    
  24.  
  25.                  //首先确定排序的趟数;  
  26.  
  27.         int max=array[0];  
  28.  
  29.         for(int i=1;i<array.length;i++){  
  30.  
  31.                     if(array[i]>max){  
  32.  
  33.                max=array[i];  
  34.  
  35.                     }  
  36.  
  37.                  }  
  38.  
  39.                    
  40.  
  41.         int time=0;  
  42.  
  43.                 //判断位数;  
  44.  
  45.                  while(max>0){  
  46.  
  47.                     max/=10;  
  48.  
  49.                      time++;  
  50.  
  51.                  }  
  52.  
  53.                    
  54.  
  55.         //建立10个队列;  
  56.  
  57.                  List<ArrayList> queue=new ArrayList<ArrayList>();  
  58.  
  59.                  for(int i=0;i<10;i++){  
  60.  
  61.                           ArrayList<Integer> queue1=new ArrayList<Integer>();
  62.  
  63.                      queue.add(queue1);  
  64.  
  65.         }  
  66.  
  67.                    
  68.  
  69.                  //进行time次分配和收集;  
  70.  
  71.                  for(int i=0;i<time;i++){  
  72.  
  73.                        
  74.  
  75.                      //分配数组元素;  
  76.  
  77.                     for(int j=0;j<array.length;j++){  
  78.  
  79.                          //得到数字的第time+1位数;
  80.  
  81.                              int x=array[j]%(int)Math.pow(10, i+1)/(int)Math.pow(10, i);
  82.  
  83.                              ArrayList<Integer> queue2=queue.get(x);
  84.  
  85.                              queue2.add(array[j]);
  86.  
  87.                              queue.set(x, queue2);
  88.  
  89.             }  
  90.  
  91.                      int count=0;//元素计数器;  
  92.  
  93.             //收集队列元素;  
  94.  
  95.                      for(int k=0;k<10;k++){
  96.  
  97.                 while(queue.get(k).size()>0){
  98.  
  99.                          ArrayList<Integer> queue3=queue.get(k);
  100.  
  101.                              array[count]=queue3.get(0);  
  102.  
  103.                              queue3.remove(0);
  104.  
  105.                     count++;
  106.  
  107.               }  
  108.  
  109.             }  
  110.  
  111.                }  
  112.  
  113.                    
  114.  
  115.    }  
  116.  
  117.  
  118.  
  119. }
  120.  

回复 "java排序之-基数排序"

这儿你可以回复上面这条便签

captcha