Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. /*
  2. For the two ways of building the heap from an unordered array, we can conclude that the Bottom-Up approach is faster.
  3. 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.
  4. In the Bottom-Up approach, the algorithm assumes that the leaves are already in place, so it only applies to n/2 nodes;
  5. running times: -Bottom-up : O(n)
  6. -Top-down : O(nlog(n))
  7. */
  8.  
  9. #ifdef _MSC_VER
  10. #define _CRT_SECURE_NO_WARNINGS
  11. #endif
  12. #include <iostream>
  13. #include <fstream>
  14. #include <stdlib.h>
  15. #include <time.h>
  16.  
  17. #define arr_size 10
  18. #define max 5000
  19.  
  20. using namespace std;
  21.  
  22. int counterCB = 0;
  23. int counterAB = 0;
  24.  
  25. //Bottom-Up
  26. void Bottom_UP(int arr[], int n, int i)
  27. {
  28. int largest = i; //init the target as root
  29. int l = 2 * i + 1;
  30. int r = 2 * i + 2;
  31. counterCB++;
  32. if (l < n && arr[l] > arr[largest])
  33. largest = l;
  34. counterCB++;
  35. if (r < n && arr[r] > arr[largest])
  36. largest = r;
  37. if (largest != i)
  38. {
  39. swap(arr[i], arr[largest]);
  40. counterAB = counterAB + 3;
  41. Bottom_UP(arr, n, largest);
  42. }
  43. }
  44.  
  45. void buildHeapBU(int arr[], int n)
  46. {
  47.  
  48. for (int i = (n / 2) - 1; i >= 0; i--)
  49. Bottom_UP(arr, n, i);
  50.  
  51. }
  52.  
  53. void sort(int arr[], int n)
  54. {
  55. buildHeapBU(arr, n);
  56. //one by one extract elements from the heap
  57. for (int i = n - 1; i >= 1; i--)
  58. {
  59. //move current root to end
  60. swap(arr[0], arr[i]);
  61.  
  62. Bottom_UP(arr, i, 0);
  63. }
  64.  
  65. }
  66.  
  67. void print_array(int arr[], int n)
  68. {
  69. for (int i = 0; i < n; i++)
  70. cout << arr[i] << " ";
  71. cout << endl;
  72. }
  73.  
  74. // Top-Down
  75. int ReturnParent(int index)
  76. {
  77. return (index - 1) / 2;
  78. }
  79. int counterAT = 0;
  80. int counterCT = 0;
  81. void Key(int arr[], int index)
  82. {
  83.  
  84. while (index > 0 && arr[ReturnParent(index)] < arr[index])
  85. {
  86. counterCT++;
  87. counterAT = counterAT + 3;
  88. swap(arr[index], arr[ReturnParent(index)]);
  89. index = ReturnParent(index);
  90. }
  91. }
  92.  
  93. void HeapInsert(int arr[], int &size)
  94. {
  95. Key(arr, size);
  96. size++;
  97. }
  98.  
  99. void buildHeap(int arr[], int n)
  100. {
  101. int size = 1;
  102. for (int i = 1; i <= n; i++)
  103. HeapInsert(arr, size);
  104. }
  105.  
  106.  
  107. int main()
  108. {
  109. int arr1[arr_size], arr2[arr_size];
  110. srand((unsigned int)time(NULL));
  111.  
  112.  
  113.  
  114.  
  115. //proof of correctness
  116. for (int i = 0; i < arr_size; i++)
  117. {
  118. arr1[i] = rand() % 1001;
  119. arr2[i] = arr1[i];
  120. }
  121.  
  122. //proof of correctness for Top-Down
  123. cout << " Top-Down: " << endl;
  124. cout << endl;
  125. cout << " The initial array is: " << endl;
  126. print_array(arr1, arr_size);
  127. buildHeap(arr1, arr_size);
  128. cout << " The heap is: " << endl;
  129. print_array(arr1, arr_size);
  130. cout << endl;
  131.  
  132.  
  133. //proof of correctness for Bottom-Up
  134. cout << " Bottom-Up:" << endl;
  135. cout << endl;
  136. cout << " The array is: \n";
  137. print_array(arr2, arr_size);
  138. cout << " The unsorted heap is: \n";
  139. buildHeapBU(arr2, arr_size);
  140. print_array(arr2, arr_size);
  141. cout << " The sorted heap is: \n";
  142. sort(arr2, arr_size);
  143. print_array(arr2, arr_size);
  144.  
  145. // average
  146.  
  147. ofstream MyFile1;
  148. MyFile1.open("MyFile1.csv");
  149. MyFile1 << "n,Bottom A Average,Bottom C Average ,Bottom T Average,Top A Average,Top C Average,Top T Average " << "\n";
  150. int arr3[max],arr4[max];
  151. for (int n = 200; n <= 5000; n = n + 200)
  152. {
  153. counterAB = 0;
  154. counterCB = 0;
  155. counterAT = 0;
  156. counterCT = 0;
  157. for (int m = 0; m < 5; m++)
  158. {
  159. for (int i = 0; i < n; i++) {
  160.  
  161. arr3[i] = rand() % max;
  162. arr4[i] = arr3[i];
  163. }
  164. buildHeapBU(arr3, n);
  165. buildHeap(arr4, n);
  166. }
  167. counterAB = counterAB / 5;
  168. counterCB = counterCB / 5;
  169. counterAT = counterAT / 5;
  170. counterCT = counterCT / 5;
  171.  
  172. MyFile1 << n << "," << counterAB << "," << counterCB << "," << counterAB + counterCB << "," << counterAT << "," << counterCT << "," << counterAT + counterCT << endl;
  173. }
  174.  
  175. return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement