• 桶排序


    从《基于比较的排序结构总结 》中我们知道:全依赖“比较”操作的排序算法时间复杂度的一个下界O(N*logN)。但确实存在更快的算法。这些算法并不是不用“比较”操作,也不是想办法将比较操作的次数减少到 logN。而是利用对待排数据的某些限定性假设 ,来避免绝大多数的“比较”操作。桶排序就是这样的原理。

    桶排序的基本思想

     假设有一组长度为N的待排关键字序列K[1....n]。首先将这个序列划分成M个的子区间(桶) 。然后基于某种映射函数 ,将待排序列的关键字k映射到第i个桶中(即桶数组B的下标 i) ,那么该关键字k就作为B[i]中的元素(每个桶B[i]都是一组大小为N/M的序列)。接着对每个桶B[i]中的所有元素进行比较排序(可以使用快排)。然后依次枚举输出B[0]....B[M]中的全部内容即是一个有序序列。

    [桶—关键字]映射函数

          bindex=f(key)   其中,bindex 为桶数组B的下标(即第bindex个桶), k为待排序列的关键字。桶排序之所以能够高效,其关键在于这个映射函数,它必须做到:如果关键字k1<k2,那么f(k1)<=f(k2)。也就是说B(i)中的最小数据都要大于B(i-1)中最大数据。很显然,映射函数的确定与数据本身的特点有很大的关系,我们下面举个例子:

    假如待排序列K= {49、 38 、 35、 97 、 76、 73 、 27、 49 }。这些数据全部在1—100之间。因此我们定制10个桶,然后确定映射函数f(k)=k/10。则第一个关键字49将定位到第4个桶中(49/10=4)。依次将所有关键字全部堆入桶中,并在每个非空的桶中进行快速排序后得到如下图所示:

    对上图只要顺序输出每个B[i]中的数据就可以得到有序序列了。

    桶排序代价分析

    桶排序利用函数的映射关系,减少了几乎所有的比较工作。实际上,桶排序的f(k)值的计算,其作用就相当于快排中划分,已经把大量数据分割成了基本有序的数据块(桶)。然后只需要对桶中的少量数据做先进的比较排序即可。

    对N个关键字进行桶排序的时间复杂度分为两个部分:

    (1) 循环计算每个关键字的桶映射函数,这个时间复杂度是O(N)。

    (2) 利用先进的比较排序算法对每个桶内的所有数据进行排序,其时间复杂度为  ∑ O(Ni*logNi) 。其中Ni 为第i个桶的数据量。

    很显然,第(2)部分是桶排序性能好坏的决定因素。尽量减少桶内数据的数量是提高效率的唯一办法(因为基于比较排序的最好平均时间复杂度只能达到O(N*logN)了)。因此,我们需要尽量做到下面两点:

    (1) 映射函数f(k)能够将N个数据平均的分配到M个桶中,这样每个桶就有[N/M]个数据量。

    (2) 尽量的增大桶的数量。极限情况下每个桶只能得到一个数据,这样就完全避开了桶内数据的“比较”排序操作。当然,做到这一点很不容易,数据量巨大的情况下,f(k)函数会使得桶集合的数量巨大,空间浪费严重。这就是一个时间代价和空间代价的权衡问题了。

    对于N个待排数据,M个桶,平均每个桶[N/M]个数据的桶排序平均时间复杂度为:

                 O(N)+O(M*(N/M)*log(N/M))=O(N+N*(logN-logM))=O(N+N*logN-N*logM)

    当N=M时,即极限情况下每个桶只有一个数据时。桶排序的最好效率能够达到O(N)。

    总结: 桶排序的平均时间复杂度为线性的O(N+C),其中C=N*(logN-logM)。如果相对于同样的N,桶数量M越大,其效率越高,最好的时间复杂度达到O(N)。 当然桶排序的空间复杂度 为O(N+M),如果输入数据非常庞大,而桶的数量也非常多,则空间代价无疑是昂贵的。此外,桶排序是稳定的。

    其实我个人还有一个感受:在查找算法中,基于比较的查找算法最好的时间复杂度也是O(logN)。比如折半查找、平衡二叉树、红黑树等。但是Hash表却有O(C)线性级别的查找效率(不冲突情况下查找效率达到O(1))。大家好好体会一下:Hash表的思想和桶排序是不是有一曲同工之妙呢?

    桶排序在海量数据中的应用

    一年的全国高考考生人数为500 万,分数使用标准分,最低100 ,最高900 ,没有小数,你把这500 万元素的数组排个序。

    分析:对500W数据排序,如果基于比较的先进排序,平均比较次数为O(5000000*log5000000)≈1.112亿。但是我们发现,这些数据都有特殊的条件:  100=<score<=900。那么我们就可以考虑桶排序这样一个“投机取巧”的办法、让其在毫秒级别就完成500万排序。

    方法:创建801(900-100)个桶。将每个考生的分数丢进f(score)=score-100的桶中。这个过程从头到尾遍历一遍数据只需要500W次。然后根据桶号大小依次将桶中数值输出,即可以得到一个有序的序列。而且可以很容易的得到100分有***人,501分有***人。

    实际上,桶排序对数据的条件有特殊要求,如果上面的分数不是从100-900,而是从0-2亿,那么分配2亿个桶显然是不可能的。所以桶排序有其局限性,适合元素值集合并不大的情况。



    用java写了个版本

     1 import com.gxf.util.Node;
     2 import com.gxf.util.Util;
     3 
     4 /**
     5  * 桶排序
     6  * @author GXF
     7  *
     8  */
     9 public class BucketSort {
    10 
    11     public static void main(String[] args) {
    12         BucketSort bucketSort = new BucketSort();
    13         int nums[]={49,38,65,97,76,13,27,49};
    14         Util.showIntArray(nums);
    15         bucketSort.bucketSort(nums);
    16         Util.showIntArray(nums);
    17     }
    18     
    19     /**
    20      * 桶排序
    21      * @param nums
    22      */
    23     public void bucketSort(int nums[]){
    24         //所有的桶
    25         Node bucketTable[] = new Node[10];
    26         for(int i = 0; i < bucketTable.length; i++){
    27             bucketTable[i] = new Node(0);
    28         }//for
    29         
    30         //进行桶排序
    31         for(int i = 0; i < nums.length; i++){
    32             Node nodeToInsert = new Node(nums[i]);
    33             //计算桶序号,索引
    34             int bucketIndex = nums[i] / 10;
    35             //如果对应的桶为空,直接添加
    36             if(bucketTable[bucketIndex].key == 0)
    37             {
    38                 bucketTable[bucketIndex].next = nodeToInsert;
    39                 bucketTable[bucketIndex].key++;
    40             }//if
    41             else{                            
    42                 Node head = bucketTable[bucketIndex];
    43                 while(head.next != null && head.next.key < nums[i])
    44                     head = head.next;
    45                 nodeToInsert.next = head.next;
    46                 head.next = nodeToInsert;
    47                 bucketTable[bucketIndex].key++;
    48             }//else
    49         }//for
    50         
    51         //将桶中的数据放到原数组中
    52         int arrayIndex = 0;
    53         for(int i = 0; i < bucketTable.length; i++){
    54             if(bucketTable[i].key != 0){
    55                 Node head = bucketTable[i].next;
    56                 while(head != null){
    57                     nums[arrayIndex++] = head.key;
    58                     head = head.next;
    59                 }//while
    60             }//if
    61         }//for
    62     }
    63     
    64     
    65 
    66 }

    针对

    一个字符数组,里面的字符可能是a-z、A-Z、0-9.现在要求对数组进行排序,要求所有小写字符放在最前面,所有大写字符放在中间,所有数字放在最后。而且各部分内部分别有序

    用桶排序实现了一下

     1 import com.gxf.util.CharNode;
     2 import com.gxf.util.Util;
     3 
     4 /**
     5  * 用桶排序对字符数组排序
     6  * 里面的字符可能是a-z、A-Z、0-9.现在要求对数组进行排序,要求所有小写字符放在最前面,所有大写字符放在中间,所有数字放在最后。而且各部分内部分别有序
     7  * @author GXF
     8  *
     9  */
    10 public class BucketSortCharArray {
    11 
    12     public static void main(String[] args) {
    13         String string = "asdfasdfa16a5f6d6546d5f46sa5d4f4F6AS5F4A6D4FFa4fa56d4fa";
    14         char array[] = string.toCharArray();
    15         BucketSortCharArray bucketSortCharArray = new BucketSortCharArray();
    16         Util.showCharArray(array);
    17         bucketSortCharArray.bucketSortCharArray(array);
    18         Util.showCharArray(array);
    19 
    20     }
    21     
    22     /**
    23      * 使用桶排序对字符数组进行排序
    24      * @param array
    25      */
    26     public void bucketSortCharArray(char array[]){
    27         //这里只需要3个桶就okay了
    28         CharNode bucketTable[] = new CharNode[3];
    29         //初始化桶
    30         for(int i = 0; i < bucketTable.length; i++){
    31             bucketTable[i] = new CharNode(' ');
    32         }//for
    33         
    34         //开始桶排序
    35         for(int i = 0; i < array.length; i++){
    36             CharNode nodeToInsert = new CharNode(array[i]);
    37             int bucketIndex = getBucketIndext(array[i]);
    38             
    39             //如果对应的桶为空
    40             if(bucketTable[bucketIndex].next == null)
    41                 bucketTable[bucketIndex].next = nodeToInsert;
    42             else{
    43                 CharNode head = bucketTable[bucketIndex];
    44                 while(head.next != null && head.next.val < array[i])
    45                     head = head.next;
    46                 nodeToInsert.next = head.next;
    47                 head.next = nodeToInsert;
    48             }//else
    49         }//for
    50         
    51         //遍历所有桶的数据,将数据放到原数组中
    52         int arrayIndex = 0;
    53         for(int i = 0; i < bucketTable.length; i++){
    54             CharNode head = bucketTable[i].next;
    55             while(head != null){
    56                 array[arrayIndex++] = head.val; 
    57                 head = head.next;
    58             }//while
    59         }
    60     }
    61     
    62     /**
    63      * a-z、A-Z、0-9
    64      * @param element
    65      * @return
    66      */
    67     private int getBucketIndext(char element){
    68         if(element >= 'a' && element <= 'z')
    69             return 0;
    70         else if(element >= 'A' && element <= 'Z')
    71             return 1;
    72         return 2;
    73     }
    74 
    75 }

    -------------------------------------------------------------我是分割线,下面是快速排序的实现---------------------------------------------------------------

     1 import com.gxf.util.Util;
     2 
     3 /**
     4  * 对字符数组进行排序
     5  * @author GXF
     6  * 里面的字符可能是a-z、A-Z、0-9.现在要求对数组进行排序,要求所有小写字符放在最前面,所有大写字符放在中间,所有数字放在最后。而且各部分内部分别有序
     7  *
     8  */
     9 public class SortCharArray {
    10 
    11     public static void main(String[] args) {
    12         String string = "affasfasdf1656546sg6a5s4g6d4F4A65FD4A6F4AF65FAFfdf46f4s6f4a6fF6AD5F46E4F3";
    13         
    14         char array[] = string.toCharArray();
    15         Util.showCharArray(array);
    16         
    17         SortCharArray sortCharArray = new SortCharArray();
    18         sortCharArray.sort(array);
    19         
    20         Util.showCharArray(array);
    21 
    22     }
    23     
    24     private void sort(char array[]){
    25         if(array == null || array.length == 0)
    26             return;
    27         sort(array, 0, array.length - 1);
    28     }
    29     
    30     public void sort(char array[], int start, int end){
    31         if(start < end){
    32             int index = partion(array, start, end);
    33             sort(array, start, index - 1);
    34             sort(array, index + 1, end);
    35         }//if
    36     }
    37     
    38     /**
    39      * 一次划分
    40      * @param array
    41      * @param start
    42      * @param end
    43      * @return
    44      */
    45     private int partion(char array[], int start, int end){
    46         char key = array[start];
    47         while(start < end){
    48             while(start < end && !compare(array, key, end))
    49                 end--;
    50             array[start] = array[end];
    51             while(start <end && compare(array, key, start))
    52                 start++;
    53             array[end] = array[start];
    54         }//while
    55         array[start] = key;
    56         return start;
    57     }
    58     
    59     /**
    60      * 比较char[position] 和 key大小
    61      * key > char[position] true else false
    62      * @param array
    63      * @param key
    64      * @param position
    65      * @return
    66      */
    67     private boolean compare(char array[], char key, int position){
    68         int keyVal = getCharIntVal(key);
    69         int positionVal = getCharIntVal(array[position]);
    70         
    71         return keyVal - positionVal > 0 ? true : false;
    72     }
    73     
    74     /**
    75      * 获取字符对应的整型值
    76      * @param charElement
    77      * @return
    78      */
    79     private int getCharIntVal(char charElement){
    80         if(charElement >= '0' && charElement <= '9')
    81             return charElement + 'Z';
    82         if(charElement >= 'a' && charElement <= 'z')
    83             return charElement - 'z';
    84         return (int)charElement;
    85     }
    86 }

     一个很形象的桶排序动态图

    http://www.cs.usfca.edu/~galles/visualization/BucketSort.html

    参考:

    http://hxraid.iteye.com/blog/647759

  • 相关阅读:
    OpenYurt v0.4.0 新特性发布:高效地管理边缘存储资源
    OpenKruise v0.9.0 版本发布:新增 Pod 重启、删除防护等重磅功能
    dubbo-go v3 版本 go module 踩坑记
    阿里云携手 VMware 共建云原生 IoT 生态,聚开源社区合力打造领域标准
    一文告诉你Java日期时间API到底有多烂
    LocalDateTime、OffsetDateTime、ZonedDateTime互转,这一篇绝对喂饱你
    全球城市ZoneId和UTC时间偏移量的最全对照表
    全网最全!彻底弄透Java处理GMT/UTC日期时间
    GMT UTC CST ISO 夏令时 时间戳,都是些什么鬼?
    如何保证Redis高可用和高并发
  • 原文地址:https://www.cnblogs.com/luckygxf/p/4666684.html
Copyright © 2020-2023  润新知