分治法求解逆序數

   在一個排列中,如果一對數的前後位置與大小順序相反,即前面的數大於後面的數,那麼它們就稱爲一個逆序。一個排列中逆序的總數就稱爲這個排列的逆序數

      求逆序數的方法很多。最容易想到的辦法是分別對序列中每一個元素求其逆序數,再求所有元素的逆序數總和,易分析得出這樣的方法其時間複雜度爲O(n2)。

      這裏介紹一種分治的方法求逆序數,其思路如下。

       我們知道在對序列進行二路歸併排序的時候,要將序列拆分成若干子序列,先將子序列排序,再合併子序列構成最終排序後的序列。二路歸併算法還有一個特點,在進行歸併操作時候的兩個子序列是有序序列,所以,我們可以利用這一點,在歸併子序列的時候,其中的子序列內部的逆序數必然是0,這時候能產生逆序數的情況必然處於子序列之間,即:“位置靠後的子序列”中的元素小於“位置靠前的子序列”的元素。例如,有子序列x:2,4,5;子序列y:1,3,6,顯然,子序列y中的元素1的逆序數爲3,子序列y中的元素3的逆序數爲2,其他元素的逆序數均爲0。通過這樣一種方法,我們可以在序列的二路歸併排序的過程中將序列的逆序數計算出來。故其時間複雜度爲O(nlogn)。

      其代碼實現如下:

     h1.h

  1. #ifndef H1_H  
  2. #define H1_H  
  3.   
  4. #include<string.h>  
  5. #include<ctype.h>  
  6. #include<malloc.h> /* malloc()等 */  
  7. #include<limits.h> /* INT_MAX等 */  
  8. #include<stdio.h> /* EOF(=^Z或F6),NULL */  
  9. #include<stdlib.h> /* atoi() */  
  10. #include<io.h> /* eof() */  
  11. #include<math.h> /* floor(),ceil(),abs() */  
  12. #include<process.h> /* exit() */  
  13.   
  14. #define MAXSIZE 100  
  15. #define TRUE 1  
  16. #define FALSE 0  
  17. #define OK 1  
  18. #define ERROR 0  
  19. #define INFEASIBLE -1  
  20. /* #define OVERFLOW -2 因爲在math.h中已定義OVERFLOW的值爲3,故去掉此行 */  
  21. typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */  
  22. typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */  
  23.   
  24.   
  25. #endif  
InversionCount.c

  1. #include "h1.h"  
  2.   
  3. int mergeCount(int a[], int low, int center, int high){  
  4.   
  5.     int count;  
  6.     int i, j, k;  
  7.   
  8.     if(low >= high){  
  9.         return 0;  
  10.     }  
  11.   
  12.     int *temp = (int *)malloc(sizeof(int)*MAXSIZE);  
  13.     if(!temp){  
  14.         printf("Error!Memorry allocation wrong!");  
  15.     }  
  16.   
  17.     count = 0;  
  18.     for(i=low,j=(center+1),k=0; i<=center&&j<=high; ){  
  19.         if(a[i]>a[j]){  
  20.             count += (center-i+1);  
  21.             temp[k++] = a[j++];  
  22.         }  
  23.         else{  
  24.             temp[k++] = a[i++];  
  25.         }  
  26.     }  
  27.     while(i <= center){  
  28.         temp[k++] = a[i++];  
  29.     }  
  30.     while(j <= high){  
  31.         temp[k++] = a[j++];  
  32.     }  
  33.   
  34.     for(i=low; i<=high; i++){  
  35.         a[i] = temp[i-low];  
  36.     }  
  37.   
  38.     free(temp);  
  39.   
  40.     return count;  
  41. }  
  42.   
  43. int inversionCount(int a[], int low, int high){  
  44.   
  45.     int center;  
  46.   
  47.     if(low < high){  
  48.         center = (low+high)/2;  
  49.         int rl, rr, r;  
  50.         rl = inversionCount(a, low, center);  
  51.         rr = inversionCount(a, center+1, high);  
  52.         r = mergeCount(a, low, center, high);  
  53.         return (rl+rr+r);  
  54.     }  
  55.     else{  
  56.         return 0;  
  57.     }  
  58. }  
  59.   
  60. int main(){  
  61.   
  62.     int a[] = { 12, 3, 7, 10, 14, 18, 19, 2, 11, 16, 17, 23, 25};  
  63.   
  64.     int i, result=0;  
  65.   
  66.     result = inversionCount(a, 1, 12);  
  67.   
  68.     printf("The numbers sorted:\n");  
  69.     for(i=1; i<=12; i++){  
  70.         printf("%d  ", a[i]);  
  71.     }  
  72.     printf("\nThe inversion count is: %d\n", result);  
  73.   
  74.     return 0;  
  75. }  

註上述測試的序列爲:
  1. {3, 7, 10, 14, 18, 19, 2, 11, 16, 17, 23, 25}排在第一位的12表示元素個數,其最終逆序數爲13.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章