/** Source code example for "A Practical Introduction to Data Structures and Algorithm Analysis, 3rd Edition (Java)" by Clifford A. Shaffer Copyright 2008-2011 by Clifford A. Shaffer */ // Sorting main function for testing correctness of sort algorithm. // To use: [+/-] // + means increasing values, - means decreasing value and no // parameter means random values; // where controls the number of objects to be sorted; // and controls the threshold parameter for certain sorts, e.g., // cutoff point for quicksort sublists. import java.io.*; public class Sortmain { static int THRESHOLD = 0; static int ARRAYSIZE = 0; static int[] POW2 = {1, 2, 4, 8, 16, 32, 64, 128, 256}; @SuppressWarnings("unchecked") // Generic array allocation static void Sort(Integer[] A) { assert THRESHOLD > 0 : "Usage: Sortmain [+/-] , " + "MUST SET THRESHOLD AS NUMBER OF BITS IN RADIX"; Integer[] temp = new Integer[A.length]; int[] count = new int[POW2[THRESHOLD]]; radix(A, temp, 16/THRESHOLD, POW2[THRESHOLD], count); } static void radix(Integer[] A, Integer[] B, int k, int r, int[] count) { // Count[i] stores number of records in bin[i] int i, j, rtok; for (i=0, rtok=1; i=0; j--) B[--count[(A[j]/rtok)%r]] = A[j]; for (j=0; j= 1 : "Usage: Sortmain [+/-] "; System.out.println("Args: " + args.length); int i; int input = 0; int currarg = 0; if (args[currarg].charAt(0) == '+') { input = 1; currarg++; } else if (args[currarg].charAt(0) == '-') { input = -1; currarg++; } ARRAYSIZE = Integer.parseInt(args[currarg++]); if (args.length > currarg) THRESHOLD = Integer.parseInt(args[currarg]); Integer[] A = new Integer[ARRAYSIZE]; System.out.println("Input: " + input + ", size: " + ARRAYSIZE + ", threshold: " + THRESHOLD); if (input == -1) for (i=0; i 0) { System.out.println("OOPS -- bad sort algorithm!"); for (int j=0; j