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.
This is the answers for B:
public class Inversion {
static int[] array1 = new int[]{15, 31, 13, 1, 9}; // Array creation static int countInversion(int x) { int count_inv = 0; for (int m = 0; m array1[n]) count_inv++; //Counting number of inversions return count_inv; } // Main method which will call the countInversion method static public void main(String... args) // Main method { System.out.println("Number of Inversions: " + countInversion(array1.length)); //Display number of inversions }
}
13. An inversion in an array A[1.. n) is a pair of indices (i, j) such that i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
