数据结构基础(15) --基数排序

来源:互联网 发布:vs2017 工具链接mysql 编辑:程序博客网 时间:2024/05/22 08:00
  基数排序是一种借助“多关键字排序”的思想来实现“单关键字排序”的内部排序算法。

实现多关键字排序通常有两种作法:

   最低位优先法(LSD)

    先对K[0]{基数的最低位}进行排序,并按 K(0) 的不同值将记录序列分成若干子序列之后,分别对 K[1] 进行排序,..., K[d-1]依次类推,直至最后对最次位关键字排序完成为止。

  最高位优先法(MSD)

    先对 K[d-1]{基数的最高位}进行排序,然后对 K[d-2]进行排序,依次类推,直至对最主位关键字 K[0] 排序完成为止。

 

百度百科对基数排序做了如下介绍:

    基数排序(radix sort)是属于“分配式排序”(distribution sort),基数排序法又称“桶子法”(bucket sort),顾名思义,它是透过键值的部份资讯,将要排序的元素分配至某些“桶”中,藉以达到排序的作用,基数排序法是属于稳定性的排序,在某些时候,基数排序法的效率高于其它的稳定性排序法。

 

链式基数排序基本步骤如下

    1.将待排序记录以数组存储[或者以指针相链,构成一个链表]

    2.”分配”时,按当前”关键字位”所取值,将记录分配到不同的”链表/链队列”(即不同的桶或堆中)中,每条链表中记录的”关键字位”相同;

    3.”收集”时,按当前关键字位取值从小到大(即将这n条链表(n的大小为基数的大小)按照编号, 依次将其中所有的元素取出)将各链表中的元素取出放入到原先的数组或链表中;

    4.对每个关键字位均重复 2) 和 3) 两步n次。

 

如采用LSD对{179, 208, 306, 93, 859, 984, 55, 9, 271, 33}(构成一个链表或者是数组)进行基数排序:

[第一步:按个位排]

 

[第二步:按十位排]


[第三步:按百位排]

 

代码实现(LSD为例):

[cpp] view plaincopy
  1. //寻找数组中最大数字的位数  
  2. template <typename Type>  
  3. unsigned int maxBits(Type *begin, Type *end)  
  4. {  
  5.     unsigned int bits = 1;  
  6.     //standard作为基准, 如果array中的元素  
  7.     //大于standard, 则bits+1  
  8.     int standard = 10;  
  9.   
  10.     for (Type *current = begin; current != end; ++current)  
  11.     {  
  12.         while (*current >= standard)  
  13.         {  
  14.             standard *= 10;  
  15.             ++ bits;  
  16.         }  
  17.     }  
  18.   
  19.     return bits;  
  20. }  
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /**说明: 
  2.   begin:数组起始 
  3.   end:数组结尾 
  4.   radix:基数 
  5.  
  6. */  
  7. #define DEBUG  
  8. template <typename Type>  
  9. void radixSort(Type *begin, Type *end, int radix)  
  10. {  
  11.     //找到数组中最大数字的位数  
  12.     int bits = maxBits(begin, end);  
  13.   
  14.     //基数为radix, 则需要radix个链表  
  15.     std::list<Type> lists[radix];  
  16.   
  17.     // 需要循环bits次  
  18.     for (int d = 0, factor = 1; d < bits; ++d, factor*=10)  
  19.     {  
  20.         //分配...  
  21.         for (Type *current = begin; current != end; ++current)  
  22.         {  
  23.             //取出相应位置上的数 (比如个位是1)  
  24.             int number = ((*current)/factor)%10;  
  25.             //则需要将之放到(分配到)标号为1的链表中  
  26.             lists[number].push_back(*current);  
  27.         }  
  28.   
  29.         //收集...  
  30.         Type *current = begin;  
  31.         //对radix个链表中的元素进行收集  
  32.         for (int i = 0; i < radix; ++i)  
  33.         {  
  34.             while (!lists[i].empty())  
  35.             {  
  36.                 *current = lists[i].front();  
  37.   
  38.                 ++ current;  
  39.                 lists[i].pop_front();  
  40.             }  
  41.         }  
  42. #ifdef DEBUG  
  43.         //打印排序的中间结果  
  44.         for (current = begin; current != end; ++ current)  
  45.         {  
  46.             cout << *current << ' ';  
  47.         }  
  48.         cout << endl;  
  49. #endif // DEBUG  
  50.     }  
  51. }  
  52.   
  53. template <typename Type>  
  54. void radixSort(Type *array, int arraySize, int radix)  
  55. {  
  56.     return radixSort(array, array+arraySize, radix);  
  57. }  

时间复杂度分析:

    设待排序列为n个记录,d个关键码,关键码的取值范围为radix,则进行链式基数排序的时间复杂度为O(d(n+radix)),其中,一趟分配时间复杂度为O(n),一趟收集时间复杂度为O(radix),共进行d趟分配和收集.


-测试代码:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int main()  
  2. {  
  3.     int array[10];  
  4.     for (int i = 0; i < 10; ++i)  
  5.     {  
  6.         array[i] = rand()%1000;  
  7.     }  
  8.     for (int i = 0; i < 10; ++i)  
  9.     {  
  10.         cout << array[i] << ' ';  
  11.     }  
  12.     cout << endl;  
  13.   
  14.     radixSort(array, 10, 10);  
  15.   
  16.     for (int i = 0; i < 10; ++i)  
  17.     {  
  18.         cout << array[i] << ' ';  
  19.     }  
  20.     cout << endl;  
  21.   
  22.     return 0;  


原文地址:http://blog.csdn.net/zjf280441589/article/details/42609453

0 0