简体   繁体   中英

Merge sort and Inversion algorithm

Okay, my question is to find the number of inversions in a given array.

After reading the inversion algorithm, I figured that i just needed to add 1 line of code to the mergesort algorithm that I had written a few days back.

This worked perfectly for small array sizes but somehow when I'm scaling the array upto a 100000 integers, the answer is incorrect

Here is the merge function to which I added that one line.

int merge(int arr[],int low,int mid,int high)
{
    int i,j,k;
    int arr1[11];
    int arr2[11];
    for(i=0;i<mid-low+1;i++)
        arr1[i]=arr[low+i];
    for(j=0;j<high-mid;j++)
        arr2[j]=arr[mid+1+j];
    arr1[i]=9999999;
    arr2[j]=9999999;    
    i=0;
    j=0;
    for(k=low;k<=high;k++)
    {
        if(arr1[i]<=arr2[j])
        {
            arr[k]=arr1[i];
            i++;
        }   
        else
        {
            {
                arr[k]=arr2[j];
                j++;
                count=count+mid-low+1-i; //Inversion counter. 
            }
        }       
    }
    return(0);  
}   

Can anyone please tell me as to what is wrong with this?

I've spent hours trying to figure it out, but have had no luck with it. Any input would be appreciated. Thanks!

long long int count[200200];

int merge()
{
       long long int a[200200],n,left,right,e,i;
       scanf("%lld",&n);
       for( i = 0 ; i< n ; i++)
       {
               scanf("%lld",&a[i]);
               count[i] = 0;
           }
    long long int size,l1,h1,l2,h2,j,k,temp[200200];
    for(size = 1 ; size < n ; size = size *2)
    {
        l1 = 0;
        k = 0;
        while(l1 + size < n)
        {
            h1 = l1 + size - 1;
            l2 = h1 + 1;
            h2 = l2 + size - 1;
            if(h2 >=n )
            h2 = n - 1;
            i =l1;
            j =l2;
            left = count[h1];
            right= count[h2];
            e = 0; 
            while( i <= h1 && j <= h2)
            {
                if(a[i] <= a[j])
                {
                    temp[k++] = a[i++];
                }
                else
                {
                    e = e + h1 - i + 1;
                    temp[k++] = a[j++];
                }
            }
            count[h2] = left + right + e;
            while(i<=h1)
                temp[k++]=a[i++];
            while(j<=h2)
                temp[k++]=a[j++];
            for( i = l1 ; i <= h2 ;i++)
        //    printf("%d ",temp[i]);
        //    printf("\n");
            l1 = h2 + 1;
        }
        for(i = l1 ; k < n ; i++)
        {
            temp[k++] = a[i];
        }
        for( i = 0 ; i < n ; i++)
        {
        //    printf(" %d = %d ",i,count[i]);
            a[i] = temp[i];
        }
    }
    printf("%lld\n\n",count[n-1]);
    return 0;
}



int main()
{

      merge(); 
      return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM