/* File: TimingTest.java - March 2013 */ package l09; import java.util.Random; import java.util.Arrays; /** * A timing test file for various integer sorting classes based on input * data that's "nearly" sorted. * * Sample usage: * * java l09.TimingTest 10000 * * @author Michael Albert * */ public class NearlySorted{ public static void main(String[] args) { int n = Integer.parseInt(args[0]); IntSorter[] sorters = new IntSorter[] { new SelectionSort(), new NaiveInsertionSort(), new InsertionSort(), new SystemSort() }; int[] a = new int[n]; Random r = new Random(); for(int i = 0; i < a.length; i++) { a[i] = i + r.nextInt(20); } for(IntSorter s : sorters) { int[] b = Arrays.copyOf(a, a.length); long start = System.currentTimeMillis(); s.sort(b); long elapsed = System.currentTimeMillis()-start; System.out.println(s.getName() + ": " + elapsed + " ms"); } } }