基數排序

 

基數排序是另外一種比較有特色的排序方式,它是怎麼排序的呢?我們可以按照下面的一組數字做出說明: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)計算在某一分位上的數據

  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排序

  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中的排序結果,對實際的數據進行排序

  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

  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,不需要全部遍歷數據

發佈了62 篇原創文章 · 獲贊 29 · 訪問量 77萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章