public static void main(String[] args) { TrialSuite suiteB = new TrialSuite(); TrialSuite suiteRBC = new TrialSuite(); TrialSuite suiteRB1toN = new TrialSuite(); int numTrials = 10; for (int d = 16; d <= 262144; d *= 2) { d = 262144 * 4; n = d - 1; System.out.println("n:" + n); for (int t = 0; t < numTrials; t++) { System.gc(); long now = System.currentTimeMillis(); btree = createBalancedTree(); long last = System.currentTimeMillis(); btree = null; suiteB.addTrial(n, now, last); System.gc(); now = System.currentTimeMillis(); tree = createRedBlackTreeAsComplete(); last = System.currentTimeMillis(); tree = null; suiteRBC.addTrial(n, now, last); System.gc(); now = System.currentTimeMillis(); tree = createRedBlackTree(); last = System.currentTimeMillis(); tree = null; System.gc(); suiteRB1toN.addTrial(n, now, last); } } System.out.println("Complete binary tree."); System.out.println(suiteB.computeTable()); System.out.println("Complete balanced binary tree."); System.out.println(suiteRBC.computeTable()); System.out.println("Balanced binary tree 1..n"); System.out.println(suiteRB1toN.computeTable()); }
/** * Run a traditional quick sort implementation. * * <p>All incoming points are assumed to be drawn from the unit square. * * @param size size of array to construct * @param set record times using this suite set * @param pts generated points whose (x,y) vals are used in input set * @param selector which pivot index method to use. */ public static void runTrialNormal(int size, TrialSuite set, IPoint[] pts, IPivotIndex selector) { Integer[] ar = new Integer[size]; for (int i = 0, idx = 0; i < pts.length; i++) { ar[idx++] = (int) (pts[i].getX() * BASE); ar[idx++] = (int) (pts[i].getY() * BASE); } // default algs.model.array.QuickSort<Integer> qs = new algs.model.array.QuickSort<Integer>(ar); qs.setPivotMethod(selector); System.gc(); long start = System.currentTimeMillis(); qs.qsort(0, size - 1); long end = System.currentTimeMillis(); set.addTrial(size, start, end); for (int i = 0; i < ar.length - 1; i++) { assert (ar[i] <= ar[i + 1]); } }
/** * Run a quick sort implementation using a fixed single thread helper. * * <p>All incoming points are assumed to be drawn from the unit square. * * @param size size of array to construct * @param set record times using this suite set * @param pts generated points whose (x,y) vals are used in input set * @param selector which pivot index method to use. * @param ratio ratio for setting threshold */ public static void runTrialOneHelper( int size, TrialSuite set, IPoint[] pts, IPivotIndex selector, int ratio) { Integer[] ar = new Integer[size]; for (int i = 0, idx = 0; i < pts.length; i++) { ar[idx++] = (int) (pts[i].getX() * BASE); ar[idx++] = (int) (pts[i].getY() * BASE); } // use fixed one-thread helper QuickSortOneHelper<Integer> qs = new QuickSortOneHelper<Integer>(ar); qs.setThresholdRatio(ratio); qs.setPivotMethod(selector); System.gc(); long start = System.currentTimeMillis(); qs.qsort(0, size - 1); long end = System.currentTimeMillis(); set.addTrial(size, start, end); for (int i = 0; i < ar.length - 1; i++) { assert (ar[i] <= ar[i + 1]); } }