Advertisement
Guest User

Untitled

a guest
Nov 4th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class SortTest<E extends Comparable<E>> {
  4. public void test(Sorter<E> sorter, E[] array) {
  5. E[] copy = array.clone();
  6. sorter.sort(copy);
  7. System.out.println(sorter);
  8. if (copy.length < 100) {
  9. for (int i = 0; i < copy.length; i++)
  10. System.out.print(copy[i] + " ");
  11. System.out.println();
  12. }
  13. }
  14.  
  15. public static void main(String[] args) {
  16. Integer[] array = { 3, 1, 4, 1, 5, 9, 2, 6 };
  17.  
  18. if (args.length > 0) {
  19. // Print out command line argument if there is one.
  20. System.out.println("args[0] = " + args[0]);
  21.  
  22. // Create a random object to call random.nextInt() on.
  23. Random random = new Random(0);
  24.  
  25. // Make array.length equal to args[0] and fill it with random
  26. // integers:
  27. array = new Integer[Integer.parseInt(args[0])];
  28. for (int i = 0; i < array.length; i++) {
  29. array[i] = random.nextInt();
  30. }
  31.  
  32. }
  33. String s = "";
  34. SortTest<Integer> tester = new SortTest<Integer>();
  35. long start = System.currentTimeMillis();
  36. tester.test(new InsertionSort<Integer>(), array);
  37. long finish = System.currentTimeMillis();
  38. s += "Time to run insertion sort: " + (finish - start) + "\n";
  39.  
  40. start = System.currentTimeMillis();
  41. tester.test(new HeapSort<Integer>(), array);
  42. finish = System.currentTimeMillis();
  43. s += "Time to run heap sort: " + (finish - start) + "\n";
  44.  
  45. start = System.currentTimeMillis();
  46. tester.test(new QuickSort<Integer>(), array);
  47. finish = System.currentTimeMillis();
  48. s += "Time to run quick sort: " + (finish - start) + "\n";
  49.  
  50. start = System.currentTimeMillis();
  51. tester.test(new MergeSort<Integer>(), array);
  52. finish = System.currentTimeMillis();
  53. s += "Time to run merge sort: " + (finish - start) + "\n";
  54. System.out.println(s);
  55.  
  56. /**
  57. * For size = 1,000
  58. * SortTest: 14
  59. * HeapSort: 2
  60. * QuickSort: 2
  61. * MergeSort: 1
  62. *
  63. * size = 10,000
  64. * SortTest: 68
  65. * HeapSort: 14
  66. * QuickSort: 20
  67. * MergeSort: 37
  68. *
  69. * size = 100,000
  70. * SortTest: 5,167
  71. * HeapSort: 41
  72. * QuickSort: 34
  73. * MergeSort: 647
  74. *
  75. * size = 1,000,000
  76. * SortTest: N/A
  77. * HeapSort: 608
  78. * QuickSort: 280
  79. * MergeSort: 85,600
  80. *
  81. * size = 10,000,000
  82. * SortTest: N/A
  83. * HeapSort: 10,690
  84. * QuickSort: 4,343
  85. * MergeSort: N/A
  86. */
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement