Advertisement
Guest User

Untitled

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