Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #include <iostream>
  2. #include "Profiler.h"
  3. #define AVG_HEAPBUILD_BU_OP "HeapBuild_BottomUp_Operations_AverageCase"
  4. #define AVG_HEAPBUILD_TD_OP "HeapBuild_TopDown_Operations_AverageCase"
  5. #define AVG_HEAPBUILD_OP "HeapBuild_Operations_AverageCase"
  6. using namespace std;
  7. Profiler profiler("HeapSort");
  8. int OP_BU, OP_TD;
  9.  
  10. void PrintArray(int* a, int n)
  11. {
  12. for (int i = 0; i < n; i++)
  13. cout << a[i] << " ";
  14. cout << "\n";
  15. }
  16.  
  17. int* generateAvgData(int n)
  18. {
  19. int* a = (int*)malloc(n * sizeof(int));
  20. FillRandomArray(a, n);
  21. return a;
  22. }
  23.  
  24. void Heapify(int* a, int n, int i)
  25. {
  26.  
  27. int root;
  28. int left = 2 * i + 1; //the left child of the node i is at (2*i+1) index
  29. int right = 2 * i + 2; //the right child of the node i is at (2*i+2) index
  30.  
  31. if (left<n && a[left]>a[i]) //we check whether or not the left child is larger than the root and if it is, the root will take the index value of the left child
  32. {
  33. root = left;
  34. OP_BU++;
  35. }
  36. else root = i;
  37. if (left < n) OP_BU++;
  38.  
  39. if (right<n && a[right]>a[root]) //we check whether or not the right child is larger than the root and if it is, the root will take the index value of the right child
  40. {
  41. root = right;
  42. OP_BU++;
  43. }
  44. if (right < n) OP_BU++;
  45.  
  46. if (root != i)
  47. {
  48.  
  49. swap(a[i], a[root]);
  50. OP_BU += 3;
  51. Heapify(a, n, root); //if the root doesn't have the same value as the initial value, then we swap the terms and recursively heapify the affected subtree
  52. }
  53.  
  54. }
  55.  
  56.  
  57. void BottomUp(int* a, int n)
  58. {
  59. for (int i = n / 2 - 1; i >= 0; i--)
  60. Heapify(a, n, i);
  61. }
  62.  
  63. void TopDown(int* a, int n, int position, int* heap)
  64. {
  65. int parent = (position - 1) / 2;
  66. int x = position;
  67. heap[position] = a[position];
  68. OP_TD++;
  69. while (position > 0 && heap[position] > heap[parent])
  70. {
  71. OP_TD++;
  72. //move parent down
  73. swap(heap[position], heap[parent]);
  74. OP_TD += 3;
  75. position = parent;
  76. parent = (position - 1) / 2;
  77. }
  78. if (position > 0) OP_TD++;
  79.  
  80. }
  81.  
  82. void HeapSort(int* a, int n)
  83. {
  84.  
  85. BottomUp(a, n);
  86. for (int i = n - 1; i >= 1; i--) //We move the root at the end and we make the lowest number the root and call the Heapify subprogram on the new heap
  87. {
  88. swap(a[0], a[i]);
  89. n--;
  90. OP_BU += 3;
  91. Heapify(a, n, 0);
  92. }
  93. }
  94.  
  95. void BottomUpDemo()
  96. {
  97. int a[7] = { 2, 5, 6, 1, 9, 4, 11 };
  98. BottomUp(a, 7);
  99. PrintArray(a, 7);
  100. }
  101.  
  102. void TopDownDemo()
  103. {
  104. int a[10] = { 2,9,7,6,5,8,10,3,6,9 };
  105. int* heap = (int*)malloc(10 * sizeof(int));
  106. for (int i = 0; i < 10; i++)
  107. TopDown(a, 10, i, heap);
  108. PrintArray(heap, 10);
  109. }
  110.  
  111. void HeapSortDemo()
  112. {
  113. int a[7] = {11, 5, 6, 1, 9, 4, 2 };
  114. HeapSort(a, 7);
  115. PrintArray(a, 7);
  116. }
  117.  
  118. void generateCharts()
  119. {
  120. for (int size = 100; size <= 10000; size += 500)
  121. {
  122. int Sum_Of_OP_BU=0, Sum_Of_OP_TD = 0;
  123. for (int i = 1; i <= 5; i++)
  124. {
  125. OP_BU = 0;
  126. OP_TD = 0;
  127. int* a = generateAvgData(size);
  128. int* b = (int*)malloc(size * sizeof(int));
  129. int* heap = (int*)malloc(size * sizeof(int));
  130. for (int j = 0; j < size; j++)
  131. b[j] = a[j];
  132. BottomUp(a, size);
  133. for (int j = 0; j < size; j++)
  134. TopDown(b, size, j, heap);
  135. free(b);
  136. Sum_Of_OP_BU += OP_BU;
  137. Sum_Of_OP_TD += OP_TD;
  138. }
  139. float media_OP_BU, media_OP_TD;
  140. media_OP_BU = (float)(Sum_Of_OP_BU / 5);
  141. media_OP_TD = (float)(Sum_Of_OP_TD / 5);
  142. profiler.countOperation(AVG_HEAPBUILD_BU_OP, size, media_OP_BU);
  143. profiler.countOperation(AVG_HEAPBUILD_TD_OP, size, media_OP_TD);
  144. profiler.createGroup(AVG_HEAPBUILD_OP, AVG_HEAPBUILD_BU_OP, AVG_HEAPBUILD_TD_OP);
  145.  
  146. }
  147. }
  148.  
  149.  
  150. int main()
  151. {
  152. BottomUpDemo();
  153. TopDownDemo();
  154. HeapSortDemo();
  155. generateCharts();
  156. profiler.showReport();
  157.  
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement