基数排序

来源:互联网 发布:网络时间校准器 编辑:程序博客网 时间:2024/04/28 15:12
 

基数排序是另外一种比较有特色的排序方式,它是怎么排序的呢?我们可以按照下面的一组数字做出说明:12、 104、 13、 7、 9

    (1)按个位数排序是12、13、104、7、9

    (2)再根据十位排序104、7、9、12、13

    (3)再根据百位排序7、9、12、13、104

    这里注意,如果在某一位的数字相同,那么排序结果要根据上一轮的数组确定,举个例子来说:07和09在十分位都是0,但是上一轮排序的时候09是排在07后面的;同样举一个例子,12和13在十分位都是1,但是由于上一轮12是排在13前面,所以在十分位排序的时候,12也要排在13前面。

    所以,一般来说,10基数排序的算法应该是这样的?

    (1)判断数据在各位的大小,排列数据;

    (2)根据1的结果,判断数据在十分位的大小,排列数据。如果数据在这个位置的余数相同,那么数据之间的顺序根据上一轮的排列顺序确定;

    (3)依次类推,继续判断数据在百分位、千分位......上面的数据重新排序,直到所有的数据在某一分位上数据都为0。

    说了这么多,写上我们的代码。也希望大家自己可以试一试。

    a)计算在某一分位上的数据

view plain
  1. int pre_process_data(int array[], int length, int weight)  
  2. {  
  3.     int index ;  
  4.     int value = 1;  
  5.   
  6.     for(index = 0; index < weight; index++)  
  7.         value *= 10;  
  8.   
  9.     for(index = 0; index < length; index ++)  
  10.         array[index] = array[index] % value /(value /10);  
  11.   
  12.     for(index = 0; index < length; index ++)  
  13.         if(0 != array[index])  
  14.             return 1;  
  15.   
  16.     return 0;  
  17. }  
    b)对某一分位上的数据按照0~10排序

view plain
  1. void sort_for_basic_number(int array[], int length, int swap[])  
  2. {  
  3.     int index;  
  4.     int basic;  
  5.     int total = 0;  
  6.   
  7.     for(basic = -9; basic < 10; basic++){  
  8.         for(index = 0; index < length; index++){  
  9.             if(-10 != array[index] && basic == array[index] ){  
  10.                 swap[total ++] = array[index];  
  11.                 array[index] = -10;  
  12.             }  
  13.         }  
  14.     }  
  15.   
  16.     memmove(array, swap, sizeof(int) * length);  
  17. }  


    c)根据b中的排序结果,对实际的数据进行排序

view plain
  1. void sort_data_by_basic_number(int array[], int data[], int swap[], int length, int weight)  
  2. {  
  3.     int index ;  
  4.     int outer;  
  5.     int inner;  
  6.     int value = 1;  
  7.   
  8.     for(index = 0; index < weight; index++)  
  9.         value *= 10;  
  10.   
  11.     for(outer = 0; outer < length; outer++){  
  12.         for(inner = 0; inner < length; inner++){  
  13.             if(-10 != array[inner] && data[outer]==(array[inner] % value /(value/10))){  
  14.                 swap[outer] = array[inner];  
  15.                 array[inner] = -10;  
  16.                 break;  
  17.             }  
  18.         }  
  19.     }  
  20.   
  21.     memmove(array, swap, sizeof(int) * length);  
  22.     return;  
  23. }  

    d)把a、b、c组合起来构成基数排序,直到某一分位上的数据为0

view plain
  1. void radix_sort(int array[], int length)  
  2. {  
  3.     int* pData;  
  4.     int weight = 1;  
  5.     int count;  
  6.     int* swap;  
  7.     if(NULL == array || 0 == length)  
  8.         return;  
  9.   
  10.     pData = (int*)malloc(sizeof(int) * length);  
  11.     assert(NULL != pData);  
  12.     memmove(pData, array, length * sizeof(int));  
  13.   
  14.     swap = (int*)malloc(sizeof(int) * length);  
  15.     assert(NULL != swap);  
  16.   
  17.     while(1){  
  18.         count = pre_process_data(pData, length, weight);  
  19.         if(!count)  
  20.             break;  
  21.   
  22.         sort_for_basic_number(pData, length, swap);  
  23.         sort_data_by_basic_number(array, pData, swap, length, weight);  
  24.         memmove(pData, array, length * sizeof(int));  
  25.         weight ++;  
  26.     }  
  27.   
  28.     free(pData);  
  29.     free(swap);  
  30.     return;  
  31. }  

总结:

    (1)测试的时候注意负数的情形

    (2)如果在某一位数据相同,那么需要考虑上一轮数据排序的情况

    (3)代码中多次分配小空间,此处代码待优化


补充:

    (1) 10月15日晚上修改了余数取值范围,这样负数也可以参加排序

    (2)10月16日上午增加了一个swap内存分配,避免了内存的重复分配和释放

    (3)10月16日上午删除了count计数,一旦发现有不等于0的数据直接返回为1,不需要全部遍历数据