$29
1 Programming Assignment
In a sequence π = π 1, π 2, … , π π of π integers, an inversion is a pair of elements π π and π π where
π < π (that is, π π appears before π π
in the sequence) and π π π π
. For example, in the sequence
π = 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 π΄ of π integers in the range 1 − π.
Output: An integer, corresponding to the number of inversions in π΄.
An array with π elements may have as many as
(
π
2
) =
π(π − 1)
2
inversions. When the number of inversions π may be any value between 0 and π(π−1)
2
, the best
algorithm for counting inversions has running time π(π log(π)). There also exists a π(π + π)
algorithm for counting inversions, which is π(π
2
) when π ∈ π(π
2
).
In this assignment, we will assume that the number of inversions is at most π (that is, π ≤ π).
When the π(π + π) algorithm is used with such values of π, its running time is π(π + π) =
π(π). For full marks, your implementation must run in π(π) time when π ≤ π.
Your task is to write a java program, stored in a file named CountInversions.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. It should also handle errors but don’t
worry about running times.
2 Examples
The table below gives the inversion count and a list of the inversions present in several small
input arrays.
Input Array # of Inversions Inversions
1, 2, 3 0 none
1, 2, 3, 5, 4 1 (5, 4)
10, 30, 40, 20, 50 3 (30, 20), (40, 20)
4, 3, 2, 1, 5, 6 6 (4, 3), (4, 2), (4, 1), (3, 2), (3, 1), (2, 1)
5, 1, 2, 3, 4 4 (5, 1), (5, 2), (5, 3), (5, 4)
3 Test Datasets
A set of input files have been uploaded to ConneX, containing arrays of various sizes and
inversion counts. You should test your implementation on the uploaded files before submitting.
Note that an algorithm with a running time which is π(π log π) or better should be able to
process any of the files within a few seconds. The uploaded files may not cover all possible
cases, so you should test your implementation on other inputs as well.
4 Evaluation Criteria
The programming assignment will be marked out of 25, based on a combination of automated
testing (using large test arrays similar to the ones posted on ConneX) and human inspection. All
of the tested input arrays will have an inversion count π which is at most π.
Score (/25) Description
0 - 5 Submission does not compile or does not
conform to the provided template.
5 - 15 The implemented algorithm is π(π
2
) or is
substantially inaccurate on the tested inputs.
15 - 20 The implemented algorithm is π(π log π) or
contains minor errors.
20 – 25 The implemented algorithm is π(π + π) on an
array with π elements and π inversions, resulting
in a π(π) algorithm when π ∈ π(π).
To be properly tested, every submission must compile correctly as submitted. If your submission
does not compile for any reason (even trivial mistakes like typos), it will receive at most 5 out of
25.