Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class SortTest<E extends Comparable<E>> {
- public void test(Sorter<E> sorter, E[] array) {
- E[] copy = array.clone();
- sorter.sort(copy);
- System.out.println(sorter);
- if (copy.length < 100) {
- for (int i = 0; i < copy.length; i++)
- System.out.print(copy[i] + " ");
- System.out.println();
- }
- }
- public static void main(String[] args) {
- Integer[] array = { 3, 1, 4, 1, 5, 9, 2, 6 };
- if (args.length > 0) {
- // Print out command line argument if there is one.
- System.out.println("args[0] = " + args[0]);
- // Create a random object to call random.nextInt() on.
- Random random = new Random(0);
- // Make array.length equal to args[0] and fill it with random
- // integers:
- array = new Integer[Integer.parseInt(args[0])];
- for (int i = 0; i < array.length; i++) {
- array[i] = random.nextInt();
- }
- }
- String s = "";
- SortTest<Integer> tester = new SortTest<Integer>();
- long start = System.currentTimeMillis();
- tester.test(new InsertionSort<Integer>(), array);
- long finish = System.currentTimeMillis();
- s += "Time to run insertion sort: " + (finish - start) + "\n";
- start = System.currentTimeMillis();
- tester.test(new HeapSort<Integer>(), array);
- finish = System.currentTimeMillis();
- s += "Time to run heap sort: " + (finish - start) + "\n";
- start = System.currentTimeMillis();
- tester.test(new QuickSort<Integer>(), array);
- finish = System.currentTimeMillis();
- s += "Time to run quick sort: " + (finish - start) + "\n";
- start = System.currentTimeMillis();
- tester.test(new MergeSort<Integer>(), array);
- finish = System.currentTimeMillis();
- s += "Time to run merge sort: " + (finish - start) + "\n";
- System.out.println(s);
- /**
- * For size = 1,000
- * SortTest: 14
- * HeapSort: 2
- * QuickSort: 2
- * MergeSort: 1
- *
- * size = 10,000
- * SortTest: 68
- * HeapSort: 14
- * QuickSort: 20
- * MergeSort: 37
- *
- * size = 100,000
- * SortTest: 5,167
- * HeapSort: 41
- * QuickSort: 34
- * MergeSort: 647
- *
- * size = 1,000,000
- * SortTest: N/A
- * HeapSort: 608
- * QuickSort: 280
- * MergeSort: 85,600
- *
- * size = 10,000,000
- * SortTest: N/A
- * HeapSort: 10,690
- * QuickSort: 4,343
- * MergeSort: N/A
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement