HeapTest.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import java.util.Arrays;
  2. public class HeapTest {
  3. public static void main(String[] args) {
  4. // int length = 2;
  5. // while (length < 10000000) {
  6. // System.out.print("Length " + length + " - ");
  7. // testHeap(length);
  8. // length *= 2;
  9. // }
  10. // O(n) - Every time length doubles, time complexity doubles with it
  11. // Length 2 - Loop went 1 times
  12. // Length 4 - Loop went 2 times
  13. // Length 8 - Loop went 3 times
  14. // Length 16 - Loop went 4 times
  15. // Length 32 - Loop went 5 times
  16. // Length 64 - Loop went 6 times
  17. // Length 128 - Loop went 7 times
  18. // Length 256 - Loop went 8 times
  19. // Length 512 - Loop went 9 times
  20. // Length 1024 - Loop went 10 times
  21. // Length 2048 - Loop went 11 times
  22. // Length 4096 - Loop went 12 times
  23. // Length 8192 - Loop went 13 times
  24. // Length 16384 - Loop went 14 times
  25. // Length 32768 - Loop went 15 times
  26. // Length 65536 - Loop went 16 times
  27. // Length 131072 - Loop went 17 times
  28. // Length 262144 - Loop went 18 times
  29. // Length 524288 - Loop went 19 times
  30. // Length 1048576 - Loop went 20 times
  31. // Length 2097152 - Loop went 21 times
  32. // Length 4194304 - Loop went 22 times
  33. // Length 8388608 - Loop went 23 times
  34. //
  35. //
  36. // BUBBLE SORT RESULTS
  37. // sortTest(20);
  38. // sortTest(50000);
  39. // //100% usage of 1 CPU core for this btw
  40. // sortTest(100000);
  41. // sortTest(200000);
  42. // Elapsed Time For Length 20: 0
  43. // Elapsed Time For Length 50000: 3052
  44. // Elapsed Time For Length 100000: 13250
  45. // Elapsed Time For Length 200000: 49951
  46. //
  47. //
  48. // HEAP SORT
  49. sortTest(20);
  50. sortTest(200000);
  51. sortTest(2000000);
  52. sortTest(20000000);
  53. sortTest(40000000);
  54. // Elapsed Time For Length 20: 0
  55. // Elapsed Time For Length 200000: 21
  56. // Elapsed Time For Length 2000000: 196
  57. // Elapsed Time For Length 20000000: 7919
  58. // Elapsed Time For Length 40000000: 24130
  59. //
  60. // Looks like heap sort is much faster than bubble sort in this instance
  61. }
  62. public static void testHeap(int length) {
  63. IntMinHeap x = new IntMinHeap();
  64. for (int random : generateRandom(length)) {
  65. x.add(random);
  66. }
  67. x.addCountedLoops(-1);
  68. }
  69. public static void sortTest(int length) {
  70. int[] array = generateRandom(length);
  71. if (length < 25) System.out.println(Arrays.toString(array));
  72. long timeOne = System.currentTimeMillis();
  73. heapSort(array);
  74. long timeTwo = System.currentTimeMillis();
  75. if (length < 25) System.out.println(Arrays.toString(array));
  76. System.out.println(
  77. "Elapsed Time For Length " + length + ": " + (timeTwo - timeOne)
  78. );
  79. }
  80. public static void heapSort(int[] array) {
  81. IntMinHeap heap = new IntMinHeap(array.length);
  82. for (int num : array) {
  83. heap.add(num);
  84. }
  85. for (int i = 0; i < array.length; i++) {
  86. array[i] = heap.remove();
  87. }
  88. }
  89. public static int[] generateRandom(int length) {
  90. int[] a = new int[length];
  91. for (int i = 0; i < length; i++) a[i] = (int) (Math.random() *
  92. length *
  93. 10);
  94. return a;
  95. }
  96. public static void bubbleSort(int[] a) {
  97. for (int i = 0; i < a.length - 1; i++) {
  98. for (int j = 1; j < a.length - i; j++) {
  99. if (a[j - 1] > a[j]) IntMinHeap.swap(a, j - 1, j);
  100. }
  101. }
  102. }
  103. }