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.

C. Implement the algorithm developed in B in either Java 8 (orThis 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 A[j]. The number of inversions in an n-element array is between 0 (if the array is sorted) and (2) (if the array is sorted backward). Describe and analyze an algorithm to count the number of inversions in an n-element array in O(nlog n) time. [Hint: Modify mergesort.]

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!