{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

testsort - System.out.println"Expected...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// Arup Guha // 9/25/06 // Written in COP 3503 class to test the theoretical result derived about // the number of inversions in a random array. / import java.util.*; import java.io.*; i public class testsort { p public static void main(String[] args) { Random r = new Random(); int cnt = 0; i // Run the test 1000 times. for (int i=0; i<1000; i++) { f // We create an array of size 1000, with numbers ranging from 0 // to 999,999. int[] test = randArray(1000, 1000000, r); i // Keep track of the total number of inversions in all test runs. cnt += countInversions(test); } // Print out the average number of inversions. System.out.println("Avg inversions = "+(double)cnt/1000); S // Print out what we expected if all values were distinct.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: System.out.println("Expected = "+(1000*999/4)); S } // Returns a random array of values of size size where each value is // in between 0 and maxval-1. r is used to create the random values. public static int randArray(int size, int maxval, Random r) { p int vals = new int[size]; i for (int i=0; i<size; i++) { vals[i] = Math.abs(r.nextInt()%maxval); } return vals; } // Returns the number of inversions in array. public static int countInversions(int array) { p int cnt = 0; for (int i=0; i<array.length; i++) for (int j=i+1; j<array.length; j++) if (array[j] < array[i]) cnt++; return cnt; } }...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online