Ejemplo n.º 1
0
void countInversions(int* A,int left,int right)
{
	int mid;
	if(left < right)
	{
		mid = (left+right)/2;
		countInversions(A,left,mid);
		countInversions(A,mid+1,right);
		countSplit(A,left,mid,right);
	}
	return;
}
int main(){
	
	int arr[] = {1, 20, 6, 4, 5};
	
	printf("\nNumber of inversions : %d",countInversions(arr, sizeof(arr)/sizeof(arr[0])));
	
	return 0;
}
Ejemplo n.º 3
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);  
 }  
Ejemplo n.º 4
0
int main(void)
{
	FILE* fp;
	fp = fopen("Integerarray","r");
	int counton = 100000;
	int val;	
	int* A = (int*)malloc(counton*sizeof(int));
	int i = 0;
	while((fscanf(fp,"%d",&A[i])) != EOF)
	{
		i++;
	}
	countInversions(A,0,counton-1);
	printf("Count = %ld\n",count);
	return 0;
}