Question: 1 Programming Assignment In a sequence S = [S1,S2, ..., Sn] of n integers, an inversion is a pair of elements si and s; where
1 Programming Assignment In a sequence S = [S1,S2, ..., Sn] of n integers, an inversion is a pair of elements si and s; where i S;. For example, in the sequence S = 2,1,5,3,4 the pairs (2,1), (5,3) and (5,4) are inversions. The programming assignment is to implement an algorithm which counts the number of inversions in an input sequence: Input: An array A of n integers in the range 1 to n. Output: An integer, corresponding to the number of inversions in A. An array with n elements may have as many as n(n-1) 2 inversions. When the number of inversions, k, may be any value between 0 and n(n-1) the best algorithm for counting inversions has running time O(n log n). There also exists a O(n + k) algorithm for counting inversions, which is 0(na) when k 0(na). 2 In this assignment, we will assume that the number of inversions is at most n (that is, k sn). When the O(n + k) algorithm is used with such values of k, its running time is 0(n + n) O(n). For full marks, your implementation must run in O(n) time when k sn. Your task is to write a java program, stord; in a file named Count Inversions.java that contains a function CountInversions, which takes an integer array A as its only argument, and returns an integer value. The main function in your code should help you test your implementation by getting test data or reading it from a file