Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. /*For the two ways of building the heap from an unordered array, we can conclude that the Bottom-Up approach is faster;
  2. This happens because in the case of the Top - Down approach, the Heapify function is called on all the nodes, after the unordered heap was built;
  3. Bottom up:
  4. - half of the array are leaves ( last half ) then we have to heapify the first n/2 values.
  5. - stability : not stable
  6.  
  7. Top down
  8. - at first we start with a heap of 1 element and then we increase the heap size and swim the greater elements upwards
  9. - stability : not stable
  10. */
  11.  
  12.  
  13. #ifdef _MSC_VER
  14. #define _CRT_SECURE_NO_WARNINGS
  15. #endif
  16. #include <iostream>
  17. #include <fstream>
  18. #include <time.h>
  19. #include <stdlib.h>
  20.  
  21. #define arr_size 10
  22.  
  23.  
  24. using namespace std;
  25.  
  26. int counterAT, counterCT, counterAB, counterCB;
  27.  
  28. void swap(int *xp, int *yp)
  29. {
  30. int temp = *xp;
  31. *xp = *yp;
  32. *yp = temp;
  33. }
  34.  
  35. //Start Bottom-Up
  36. void Bottom_Up(int arr[], int n, int root)
  37. {
  38. int largest = root;
  39. int left = 2 * root + 1;
  40. int right = 2 * root + 2;
  41.  
  42. counterCB++;
  43. if(left < n && arr[largest] < arr[left])
  44. {
  45. largest = left;
  46. }
  47.  
  48. counterCB++;
  49. if (right < n && arr[largest] < arr[right])
  50. {
  51. largest = right;
  52. }
  53.  
  54. if (largest != root)
  55. {
  56. swap(arr[root], arr[largest]);
  57. counterAB += 3;
  58. Bottom_Up(arr, n, largest);
  59. }
  60. }
  61.  
  62.  
  63. void BuildHeapBU(int arr[], int n)
  64. {
  65.  
  66. for (int i = (n / 2) - 1; i >= 0; i--)
  67. {
  68. Bottom_Up(arr, n, i);
  69. }
  70. }
  71.  
  72. void HeapSortBu(int arr[], int n)
  73. {
  74. BuildHeapBU(arr, n);
  75. for ( int i = n - 1; i >= 1; i--)
  76. {
  77. swap(arr[0], arr[i]);
  78. Bottom_Up(arr, i, 0);
  79. }
  80. }
  81.  
  82. void print_array(int arr[], int n)
  83. {
  84. for (int i = 0; i < n; i++)
  85. {
  86. cout << arr[i] << " ";
  87.  
  88. }
  89. cout << "\n";
  90. }
  91.  
  92.  
  93. // END BOTTOM-UP
  94.  
  95. //START TOP-DOWN
  96.  
  97. void RecursiveSwim(int arr[], int index)
  98. {
  99. if (index == 0) return;
  100. else
  101. {
  102. int parent = (index - 1) / 2;
  103.  
  104. counterCT++;
  105.  
  106. if (arr[parent] < arr[index])
  107. {
  108. int buff = arr[index];
  109. arr[index] = arr[parent];
  110. arr[parent] = buff;
  111.  
  112. counterAT += 3;
  113.  
  114. RecursiveSwim(arr, parent);
  115. }
  116. }
  117.  
  118. }
  119.  
  120.  
  121. void BuildheapTD(int arr[], int n)
  122. {
  123.  
  124. for (int heapsize = 1; heapsize < n; heapsize++)
  125. {
  126. RecursiveSwim(arr, heapsize);
  127. }
  128. }
  129.  
  130. // END TOP-DOWN
  131.  
  132.  
  133.  
  134.  
  135.  
  136. int main()
  137. {
  138. ofstream MyFile;
  139. MyFile.open("MyFile.csv");
  140. MyFile << "n,BU A average, BU C Average,BU T Average,TD A Average,TD C Average,TD T Average" << "\n";
  141. int arr1[Max_Size], arr2[Max_Size];
  142. srand((unsigned int)time(NULL));
  143.  
  144. //Average case
  145. for (int n = 200; n <= 5000; n = n + 200)
  146. {
  147. for (int i = 0; i < n; i++)
  148. {
  149. arr1[i] = rand() % Max_Size;
  150. arr2[i] = arr1[i];
  151. }
  152.  
  153.  
  154. counterAT = 0;
  155. counterCT = 0;
  156. counterAB = 0;
  157. counterCB = 0;
  158.  
  159. BuildHeapBU(arr1, n);
  160. BuildheapTD(arr2, n);
  161.  
  162. MyFile << n << "," << counterAB << "," << counterCB << "," << counterAB + counterCB << "," << counterAT << "," << counterCT << "," << counterAT + counterCT <<endl;
  163.  
  164. }
  165. //Best Case
  166. MyFile << "n, BU A Best, BU C Best, BU T Best,TD A Best, TD C Best, TD T Best " << "\n";
  167. for (int n = 200; n <= 5000; n = n + 200)
  168. {
  169. for (int i = 0; i < n; i++)
  170. {
  171. arr1[i] = i;
  172.  
  173. }
  174. counterAB = 0;
  175. counterCB = 0;
  176. BuildHeapBU(arr1, n);
  177.  
  178. for (int i = 0; i < n; i++)
  179. {
  180. arr2[i] = i;
  181. counterAT = 0;
  182. counterCT = 0;
  183. BuildheapTD(arr2, n);
  184. }
  185. MyFile << n << "," << counterAB << "," << counterCB << "," << counterAB + counterCB << "," << counterAT << "," << counterCT << "," << counterAT + counterCT << endl;
  186. }
  187.  
  188. //Worst Case
  189. MyFile << "n, BU A Worst, BU C Worst, BU T Worst, TD A Worst, TD C Worst,TD T Worst" << "\n";
  190. for (int n = 200; n <= 5000; n = n + 200)
  191. {
  192.  
  193. for (int i = 0; i < n; i++)
  194. {
  195. arr1[i] = n - i + 1;
  196. counterAB = 0;
  197. counterCB = 0;
  198. BuildHeapBU(arr1, n);
  199. }
  200.  
  201. for (int i = 0; i < n; i++)
  202. {
  203. arr2[i] = n - i + 1;
  204. counterAT = 0;
  205. counterCT = 0;
  206. BuildheapTD(arr2, n);
  207. }
  208.  
  209. MyFile << n << "," << counterAB << "," << counterCB << "," << counterAB + counterCB << "," << counterAT << "," << counterCT << "," << counterAT + counterCT << endl;
  210. }
  211.  
  212. MyFile.close();
  213.  
  214. //correctness of Bottom-Up
  215. cout << "Array1:\n";
  216. int n = 9;
  217. for (int i = 1; i <= n; i++)
  218. arr1[i] = rand() % Max_Size;
  219. for (int i = 1; i <= n; i++)
  220. cout << arr1[i] << " ";
  221. cout << endl;
  222.  
  223. cout << "Bottom-Up Heap:\n";
  224. BuildHeapBU(arr1, n);
  225. for (int i = 1; i <= n; i++)
  226. cout << arr1[i] << " ";
  227. cout << endl;
  228.  
  229. //correctness of Top-Down
  230. cout << "Array2:\n";
  231.  
  232. for (int i = 1; i <= n; i++)
  233. arr2[i] = rand() % Max_Size;
  234. for (int i = 1; i <= n; i++)
  235. cout << arr2[i] << " ";
  236. cout << endl;
  237.  
  238. cout << "Top-Down Heap:\n";
  239. BuildHeapBU(arr1, n);
  240. for (int i = 1; i <= n; i++)
  241. cout << arr2[i] << " ";
  242. cout << endl;
  243.  
  244.  
  245.  
  246. return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement