Sudhagar Sachin
Sudhagar Sachin

Reputation: 565

Calculating Inversions?

I have this code which calculates the number of inversions in an array. It works fine for small arrays.

But for arrays of size more than 500,the value differs by 20 -50 from the correct value

def merge(left,right):
    result=[]
    i,j,inv=0,0,0
    while i<len(left) and j<len(right):
        if left[i]<right[j]:
            result.append(left[i])
            i=i+1
        else:
            result.append(right[j])
            j=j+1
            inv=inv+len(left)-i 
    result+=left[i:]    
    result+=right[j:]
    return result,inv


def mergesort(li):
    if len(li)<2:
        return li,0
    middle=len(li)//2
    left,invl=mergesort(li[:middle])     
    right,invr=mergesort(li[middle:])
    result,invs= merge(left,right)
    inv=invl+invr+invs
    return result,inv




 if __name__ == '__main__':
    n=int(raw_input())
    ans=[]
    for i in range(n):
        m=int(raw_input())
        li=raw_input().split(' ')
        print len(li)
        result,inversions=mergesort(li) 
        ans.append(inversions)
    for i in range(n):
        print ans[i]    

What is it that I am missing?

Upvotes: 0

Views: 195

Answers (1)

Daniel Fischer
Daniel Fischer

Reputation: 183918

You don't need large arrays to get a wrong inversion count:

>>> mergesort([1,1,1,1])
([1, 1, 1, 1], 6)

Your mistake is that you count all pairs of equal elements as inversions,

if left[i]<right[j]:
    result.append(left[i])
    i=i+1

should be

if left[i] <= right[j]:
    result.append(left[i])
    i=i+1

so that equal elements are not swapped and counted as inversions.

The short arrays you received contained no duplicates, but the larger ones did.

Upvotes: 4

Related Questions