/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package insertionsort; /** * * @author suchenek */ public class InversionCounter { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int[] Array; int N; for (N = 1; N<=100; N++) { Array = new int[N]; for (int i = 0; i < N; i++) Array[i] = (int)(Math.sqrt(97)*(13*i + 1)*(19*i + 2)+i - 17)%1000; // for (int i = 0; i < N; i++) // Array[i] = -i; int[] ReversedArray = new int[N]; for (int i = 0; i < N; i++) ReversedArray[i] = Array[N-i -1]; int invA = InversionCounter.Go(Array); int invR = InversionCounter.Go(ReversedArray); System.out.println("Array[" + N + "] has " + invA + " inversions out of max " + (N*(N-1)/2)); System.out.println("ReversedArray[" + N + "] has " + invR + " inversions out of max " + (N*(N-1)/2)); System.out.println("Both arrays have a total of " + (invA + invR) + " inversions out of max " + (N*(N-1)/2)); System.out.println(); } } public static int Go(int [] A) { int n = A.length; if (n <= 1) return 0; int count = 0; for (int i = 0; i < n-1; i++) { for (int j = i+1; j < n; j++) { if (A[i] > A[j]) count++; } } return count; } }