import java.util.*; import etm.core.configuration.*; import etm.core.monitor.EtmMonitor; import etm.core.monitor.EtmPoint; import etm.core.renderer.*; public class Methods { final static Random die = new Random( 1954 ); // simple testing of the select algorithm public static void main( String[] args ) { // determine whethers it uppercase or lowercase select boolean lowercaseSelect; try { int[] a = { 1, 2, 3, 4, 5, 6, 7, 8 }; Select.Select(a, 1, 4, 1 ); lowercaseSelect = false; } catch ( NoSuchMethodError e ) { lowercaseSelect = true; } // determine what kth value means int offset = 0; int[] ak = Methods.randomize( 200 ); int[] ck = Methods.duplicate(ak); Arrays.sort(ck); int vk; int nk = ak.length; if ( lowercaseSelect ) { vk = Select.select( ak, 0, nk-1, nk/4 ); } else { vk = Select.Select( ak, 0, nk-1, nk/4 ); } for (int i = -1; i <= 1; ++i) { if ( vk == ck[nk/4 + i] ) { offset = i; break; } } int[] cases = { 200, 1000, 5000, 10000, 50000 }; int[] kValue = new int[5]; BasicEtmConfigurator.configure(); EtmMonitor etm = EtmManager.getEtmMonitor(); etm.start(); final int TRIALS_PER_CASE = 5; int[][][] a = new int[ TRIALS_PER_CASE ][ kValue.length ][]; int[][] b = new int[ TRIALS_PER_CASE ][]; int[][] c = new int[ TRIALS_PER_CASE ][]; EtmPoint pointa = etm.createPoint( "herer" ); for (int j = 0; j < cases.length; ++j ) { // create possible k values int n = cases[j]; kValue[0] = 1; kValue[1] = n / 10; kValue[2] = n / 3; kValue[3] = n / 2; kValue[4] = 3 * n / 4; // create test arrays now so they are not included in the timing for (int trial = 0; trial < TRIALS_PER_CASE ; ++ trial ) { a[ trial ][ 0 ] = Methods.randomize( n ); c[ trial ] = Methods.duplicate( a[ trial ][ 0 ] ); Arrays.sort( c[trial] ); for (int i = 0; i < kValue.length; ++i ) { a[ trial ][ i ] = Methods.duplicate( a [ trial ][ 0 ] ); } } // start timing EtmPoint point = etm.createPoint( "case " + j+1 + ": n = " + n ); for (int i = 0; i < kValue.length; ++i ) { int k = kValue[i]; int decrement = 0; for (int trial = 0; trial < TRIALS_PER_CASE ; ++ trial ) { if ( isSorted( a[ trial ] [ i ] ) ) System.out.println( "sorted" ); int v; // run whatever select they have if ( lowercaseSelect ) { v = Select.select( a[trial][i], 0, a[trial][i].length - 1, k ); } else { v = Select.Select( a[trial][i], 0, a[trial][i].length - 1, k ); } // is the answer correct if ( v != c[trial][k + offset] ) { System.out.println("Failed test"); System.out.println( "n: " + n + ", k: " + k + " " + "got " + v + " expected " + c[trial][k + offset] ); Methods.println( a[trial][i] ); System.out.println( "\n" ); // array after select manipulation Methods.println( c[trial] ); System.out.println( "\n" ); // array sorted System.exit(k); } } } // end timing for series and display result point.collect(); System.out.print(n + ", " + point.getTransactionTime() / ( TRIALS_PER_CASE * kValue.length) ); if ( j + 1 == cases.length) { System.out.println( ", " + args[0] ); } else { System.out.println(); } } } // returns an array of length m with elements randomly from 0 to n-1 public static int[] randomize(int m, int n) { int[] a = new int[m]; for (int i = 0; i < m; ++i) { a[i] = die.nextInt( n ); } return a; } // returns an array of length m with elements randomly from 0 to largest positive integer public static int[] randomize(int m) { return randomize(m, Integer.MAX_VALUE ); } // scrambles a public static void randomize(int[] a) { for (int i = a.length - 1; i > 0; --i) { int j = die.nextInt(i + 1); int rmbr = a[i]; a[i] = a[j]; a[j] = rmbr; } } // returns an array of length m where ith element equals i + n public static int[] identity(int m, int n) { int[] a = new int[m]; for (int i = 0; i < m; ++i) { a[i] = i + n; } return a; } // returns an array of length m where ith element equals i public static int[] identity(int m) { return identity(m, 0); } // prints array elements with indices in the inclusive range left to right public static void print(int[] a, int left, int right) { for (int i = left; i <= right; ++i) { System.out.print( a[i] + " " ); } } // prints all of the array elements public static void print(int[] a) { print( a, 0, a.length - 1); } // prints all of the array elements and does a newline public static void println(int[] a) { println(a, 0, a.length - 1); } // prints array elements with indices in the inclusive range left to right and then does a newline public static void println(int[] a, int left, int right) { print(a, left, right); System.out.println(); } // returns a duplicate of array a public static int[] duplicate(int a[] ) { int[] b = new int[ a.length ]; for (int i = 0; i < b.length; ++i) { b[i] = a[i]; } return b; } // displays crucial debugging information from a select algorithm public static void debug(int[] a, int left, int right, int k) { System.out.println( "k: " + k + " " + "left: " + left + " " + "right: " + right ); println( a, left, right ); } // returns whether the array is sorted public static boolean isSorted(int[] a) { for (int i = 1; i < a.length; ++i) { if ( a[i-1] > a[i] ) { return false; } } return true; } }