Exemplo n.º 1
0
void mergeSort(int arr[], int l, int r)
{
    if(l < r)
    {
        int m = (l+r)/2;
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);
        mergeAndCount(arr, l, m, r);
    }
}
Exemplo n.º 2
0
 public static int countInversions(int nums[])
 {  
     int mid = nums.length/2, k;
     int countLeft, countRight, countMerge;
     if (nums.length <= 1) 
        return 0;
     int left[] = new int[mid];
     int right[] = new int[nums.length - mid];
     for (k = 0; k < mid; k++)
         left[k] = nums[k];
     for (k = 0; k < nums.length - mid; k++)
         right[k] = nums[mid+k];
     countLeft = countInversions (left);
     countRight = countInversions (right);
     int result[] = new int[nums.length];
     countMerge = mergeAndCount (left, right, result);
     for (k = 0; k < nums.length; k++)
         nums[k] = result[k];
     return (countLeft + countRight + countMerge);  
 }