zoukankan      html  css  js  c++  java
  • Java实现基于桶式排序思想和计数排序思想实现的基数排序

    计数排序

      前提:待排序表中的所有待排序关键字必须互不相同;

      思想:计数排序算法针对表中的每个记录,扫描待排序的表一趟,统计表中有多少个记录的关键码比该记录的关键码小,假设针对某一个记录,统计出的计数值为c,则该记录在新的有序表中的存放位置即为c。

      性能:空间复杂度:o(n);时间复杂度:o(n^2);

     1   public int[] countSort(int[] array){
     2         int[] tempArray = new int[array.length];  //引入辅助数组
     3         for(int i=0;i<array.length;i++){
     4             int count = 0;
     5             for(int j=0;j<array.length;j++){
     6                 if(array[i]>array[j]){
     7                     count++;
     8                 }
     9             }
    10             tempArray[count] = array[i];
    11         }
    12         return tempArray;
    13     }

    桶式排序

      桶式排序需要待排序的序列满足以下两个特征:

        待排序列所有的值处于一个可枚举的范围之类;

        待排序列所在的这个可枚举的范围不应该太大,否则排序开销太大。  

      排序的具体步骤如下:

        (1)对于这个可枚举范围构建一个buckets数组,用于记录“落入”每个桶中元素的个数;

        (2)将(1)中得到的buckets数组重新进行计算,按如下公式重新计算:

    buckets[i] = buckets[i] +buckets[i-1] (其中1<=i<buckets.length);
     1    public static void bucketSort(int[] data) {  
     2         //得到待排序元素中的最大值和最小值
     3         int max=data[0],min=data[0];
     4         for(int i=1;i<data.length;i++){
     5             if(data[i]>max){
     6                 max = data[i];
     7             }
     8             if(data[i] < min){
     9                 min = data[i];
    10             }
    11         }
    12         
    13         // 缓存数组  
    14         int[] tmp = new int[data.length];  
    15         // buckets用于记录待排序元素的信息  
    16         // buckets数组定义了max-min+1个桶  
    17         int[] buckets = new int[max-min+1];  
    18         // 计算每个元素在序列出现的次数  
    19         for (int i = 0; i < data.length; i++) {  
    20             buckets[data[i] - min]++;  
    21         }  
    22         // 计算“落入”各桶内的元素在有序序列中的位置  
    23         for (int i = 1; i < max - min; i++) {  
    24             buckets[i] = buckets[i] + buckets[i - 1];  
    25         }  
    26         // 将data中的元素完全复制到tmp数组中  
    27         System.arraycopy(data, 0, tmp, 0, data.length);  
    28         // 根据buckets数组中的信息将待排序列的各元素放入相应位置  
    29         for (int k = data.length - 1; k >= 0; k--) {  
    30             data[--buckets[tmp[k] - min]] = tmp[k];  
    31         }  
    32     }  

    基于桶式排序思想和计数排序思想实现基数排序:

      将待排序元素中的每组关键字依次进行桶分配。

     1   public int[] radixSortBuckets(int[] array, int radix) {
     2         // 找到待排序序列中的最大元素
     3         int max = array[0];
     4         for (int i = 1; i < array.length; i++) {
     5             if (array[i] > max) {
     6                 max = array[i];
     7             }
     8         }
     9         // 确定最大元素的位数maxBits
    10         int maxBits = 0;
    11         while (max > 0) {
    12             max = max/10;
    13             maxBits++;
    14         }
    15         
    16         int[] tempArray = new int[array.length];  //用于暂存元素
    17         int[] buckets = new int[radix];  //用于桶式排序
    18         int rate = 1;
    19         // 进行maxBits次分配和收集
    20         for(int i=0; i< maxBits;i++){
    21             // 将array中的元素完全复制到arrayTemp数组中
    22             System.arraycopy(array, 0, tempArray, 0, array.length);
    23             Arrays.fill(buckets, 0); // 重置buckets数组
    24             
    25             //分配:计算每个待排序元素的子关键字,并将其次数加到对应的桶中
    26             for(int j=0;j<tempArray.length;j++){
    27                 buckets[(tempArray[j]/rate)%radix] ++;
    28             }
    29             // 计算“落入”各桶内的元素在有序序列中的位置
    30             for(int k=1;k<buckets.length;k++){
    31                 buckets[k] = buckets[k]+buckets[k-1];
    32             }
    33             
    34             // 收集:按子关键字对指定的数据进行排序
    35             for(int m=tempArray.length-1;m>=0;m--){
    36                 int subKey = (tempArray[m]/rate)%radix;
    37                 array[--buckets[subKey]] = tempArray[m];
    38             }
    39             
    40             rate *= radix;
    41         }
    42         return array;
    43     }
  • 相关阅读:
    nyoj 599-奋斗的小蜗牛 (double ceil(); (temp
    nyoj 596-谁是最好的Coder (greater, less)
    nyoj 517-最小公倍数 (python range(start, end) range(length))
    用深度学习预测专业棋手走法
    阿里AI设计师一秒出图,小撒连连惊呼,真相是...
    想成为数据科学家?先做到这6点吧!
    Kubernetes 弹性伸缩全场景解析 (一)- 概念延伸与组件布局
    机器学习基础:(Python)训练集测试集分割与交叉验证
    Data Lake Analytics + OSS数据文件格式处理大全
    聊聊Flexbox布局中的flex的演算法
  • 原文地址:https://www.cnblogs.com/CherishFX/p/4650691.html
Copyright © 2011-2022 走看看