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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started