/* This is a program to count the number of inversions in a
* permutation of integers. Actually this will process several
* arrays.
*
* The input should be in the following format:
* L
* n_1 n_2 ..... n_L
* L
* n_1 n_2 ..... n_L
* ....
* ....
* 0
*
* Each list consists of L integers (L can vary from list to list).
* The elements of each list are contained in a single line.
*
* After the final list, the input is terminated with a 0 (i.e. L=0)
* to indicate the end of the input.
*
* Note: I'm assuming that the integers in each list are distinct (but
* they need not necessarily be the consecutive integers 1, 2, ..., n).
* Results might be unpredictable (depending upon how you implement
* your methods) if there are duplicated items in a list.
*
* It's easiest if you redirect input from a file (ask me if you
* don't know how to do this). For example you can try this command:
* % java Inversions <inputfile
*
* RAM 8 February 2009
*/
import java.io.*;
import java.util.*;
class Inversions
{
public static void main (String args[]) // entry point from OS
{
Scanner s, ls;
int L, listNum = 1;
s = new Scanner(System.in); // create new input Scanner
ls = new Scanner(s.nextLine());
L = ls.nextInt();
while(L > 0) /* While there's more data to process... */
{
/* Create an array to hold the integers */
int nums[] = new int[L];
/* Read the integers */
ls = new Scanner(s.nextLine());
for (int j = 0; j < L; j++)
nums[j] = ls.nextInt();
/* Compute the number of inversions, and print it out */
System.out.print( "List " + listNum + " has " );
System.out.println ( countInversions(nums) + " inversions.");
/* Read in the next value of L */
listNum++;
ls = new Scanner(s.nextLine());
L = ls.nextInt();
}
} /* end of "main" procedure */
public static int countInversions(int nums[])
/* Your task here is to create the function (or functions)
that will count the number of inversions in the input
array. The intention is that you perform a modified
MergeSort, like we discussed in class.
You might first consider just writing a plain MergeSort
routine, and then modify it later to count the inversions.
*/
{
return 0; /* Obviously you need to modify this "return" statement. */
} /* end of "countInversions" procedure */
} /* end of "Inversions" program */