import java.lancs.*; import java.util.*; //import java.lang.*; public class Test { static int A[] = new int[1000]; static double log2(double x){return Math.log(x)/Math.log(2.0);} static double twoD(double x){ x = x + 0.00001; int i = (int)(x*100); return (double)(i/100.0); } static void swap(int i, int j){ int temp = A[i]; A[i] = A[j]; A[j] = temp; } static long doTimedSwaps(long n){ Calendar mycal1 = Calendar.getInstance(); Date x = mycal1.getTime(); long start = x.getTime(); while (n>0){ for (int i=0;i<990 && n>0; i++) swap(i,i+1); n = n-990; } Calendar mycal2 = Calendar.getInstance(); Date y = mycal2.getTime(); long stop = y.getTime(); return stop - start; } public static void main(String[] args) throws Exception { for (int i=0;i<1000;i++) A[i] = i; String commands = "end,help,swaps,n"; System.out.println("\n Complexity Tester \n" + commands + "\n"); BasicIo.prompt("> "); String command = BasicIo.readString(); long n; // NOTE: int won't do!! while (!command.equals("end")){ if (command.equals("help")) System.out.println(commands); if (command.equals("swaps")){ BasicIo.prompt(">> "); n = BasicIo.readInteger(); System.out.println(doTimedSwaps(n) + " milliseconds"); } if (command.equals("n")) { BasicIo.prompt(">> "); n = BasicIo.readInteger(); System.out.println("n=" + n + " " + "n^2=" + n*n + " " + "log(n)=" + twoD(log2(n)) + " " + "n.log(n)=" + twoD(n*log2(n)) + " " + "speedUp=" + twoD((n*n)/(n*log2(n))) ); } BasicIo.prompt("> "); command = BasicIo.readString(); } } }