@@ -100,7 +100,7 @@ public double distancef(Permutation p1, Permutation p2) {
100100 w [arrayP2 [i ]] = weights [p2 .get (i )];
101101 }
102102
103- return countWeightedInversions (arrayP2 , w );
103+ return countWeightedInversions (arrayP2 , w , 0 , arrayP2 . length - 1 );
104104 }
105105
106106 /**
@@ -115,15 +115,21 @@ public double maxf(int length) {
115115 return maxDistance ;
116116 }
117117
118- private double countWeightedInversions (int [] array , double [] w ) {
119- if (array .length <= 1 ) return 0 ;
120- int m = array .length >> 1 ;
121- int [] left = Arrays .copyOfRange (array , 0 , m );
122- int [] right = Arrays .copyOfRange (array , m , array .length );
123- double weightedCount = countWeightedInversions (left , w ) + countWeightedInversions (right , w );
118+ private double countWeightedInversions (int [] array , double [] w , int first , int last ) {
119+ if (last <= first ) {
120+ return 0 ;
121+ }
122+ int m = (first + last ) >> 1 ;
123+ return countWeightedInversions (array , w , first , m ) + countWeightedInversions (array , w , m +1 , last ) + merge (array , w , first , m +1 , last +1 );
124+ }
125+
126+ private double merge (int [] array , double [] w , int first , int midPlus , int lastPlus ) {
127+ int [] left = Arrays .copyOfRange (array , first , midPlus );
128+ int [] right = Arrays .copyOfRange (array , midPlus , lastPlus );
124129 int i = 0 ;
125130 int j = 0 ;
126- int k = 0 ;
131+ int k = first ;
132+ double weightedCount = 0 ;
127133 while (i < left .length && j < right .length ) {
128134 if (left [i ] < right [j ]) {
129135 array [k ] = left [i ];
0 commit comments