Question: C. Implement the algorithm developed in B in either Java 8 (or higher) or Python 3.7 (or higher). Read the input from a text file

C. Implement the algorithm developed in B in either Java 8 (or higher) or Python 3.7 (or higher). Read the input from a text file called inversionsinput.txt. This file should contain n lines where each line contains a single integer. The program should write a single line of descriptive output with the number of inversions found. For example:

The array contains 28 inversions. 

The program must be in a single file called either CountInversions.java or countinversions.py.

The following is B.

merge(arr,left right)

{

i=0,j=0,count=0

while( i

{

if (i ==left.length)

{

arr [ i+j ] = right[ j ]

j++

}

else if( j == right.length)

{

arr[ i+j ] = left[ i ]

i++

}

else if( left[i] <= right[ j ] )

{

arr[i+j] = left[ i ]

i++

}

else

{

arr[i+j] = right[j]

count= count+left.length - i

j++

}

}

return count

}

inv_count(arr)

{

if (n <2)

return 0;

else

mid = (n+1)/2

left[] = copy_array(arr,0,mid)

right[] = copy_array(arr,mid+1,n,)

return inv_count(left) + inv_count(rught) + merge(arr,left,right)

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!