Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. // lab02_take1.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3. #ifdef _MSC_VER
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #endif
  6. #include <iostream>
  7. #include <fstream>
  8. #include <stdlib.h>
  9. #include <time.h>
  10.  
  11. #define ARR_SIZE 10
  12.  
  13. using namespace std;
  14.  
  15. int counterCBU = 0;
  16. int counterABU = 0;
  17.  
  18. //B O T T O M U P
  19. void heapify_BU(int arr[], int n, int i)
  20. {
  21. int largest = i; //init the target as root
  22. int l = 2 * i + 1;
  23. int r = 2 * i + 2;
  24. counterCBU++;
  25. if (l < n && arr[l] > arr[largest])
  26. largest = l;
  27. counterCBU++;
  28. if (r < n && arr[r] > arr[largest])
  29. largest = r;
  30. if (largest != i)
  31. {
  32. swap(arr[i],arr[largest]);
  33. counterABU = counterABU + 3;
  34. heapify_BU(arr, n, largest);
  35. }
  36. }
  37.  
  38. void buildHeap_BottomUp(int arr[], int n)
  39. {
  40. //start from the se
  41. for (int i = (n / 2)-1; i >= 0; i--)
  42. heapify_BU(arr, n, i);
  43.  
  44. }
  45.  
  46. void heapSort_BU(int arr[], int n)
  47. {
  48. buildHeap_BottomUp(arr, n);
  49. //one by one extract elements from the heap
  50. for (int i = n - 1; i >= 1; i--)
  51. {
  52. //move current root to end
  53. swap(arr[0], arr[i]);
  54. //heapify the reduced heap
  55. heapify_BU(arr, i, 0);
  56. }
  57.  
  58. }
  59.  
  60. void printArray(int arr[], int n)
  61. {
  62. for (int i = 0; i < n; i++)
  63. cout << arr[i] << " ";
  64. cout << endl;
  65. }
  66.  
  67. // T O P D O W N
  68. int retParent(int i)//returns the parent of a node
  69. {
  70. return (i - 1) / 2;
  71. }
  72. int counterATD = 0;//counter for assignments
  73. int counterCTD = 0;//counter for comparisons
  74. void heapIncreaseKey(int arr[], int i)
  75. {
  76.  
  77. while (i > 0 && arr[retParent(i)] < arr[i])
  78. {
  79. counterCTD++;
  80. counterATD = counterATD + 3;
  81. swap(arr[i], arr[retParent(i)]);
  82. i = retParent(i);
  83. }
  84. }
  85.  
  86. void maxHeapInsert(int arr[], int &heapSize)
  87. {
  88. heapIncreaseKey(arr, heapSize);
  89. heapSize++;
  90. }
  91. // n - array length
  92. void buildMaxHeap(int arr[], int n)
  93. {
  94. int heapSize = 1;
  95. for (int i = 1; i <= n; i++)
  96. maxHeapInsert(arr, heapSize);
  97. }
  98.  
  99. /*void averageCase(int incr, int max)
  100. {
  101. for (int i = incr; i <= max; i = i + incr)
  102. {
  103.  
  104. }
  105. }*/
  106.  
  107. int main()
  108. {
  109. int a[ARR_SIZE], b[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. a[i] = rand() % 1001;
  119. b[i] = a[i];
  120. }
  121.  
  122. //proof of correctness for Top-Down
  123. cout << " Top - Down " << endl;
  124. cout << endl;
  125. cout << " The initial array: " << endl;
  126. printArray(a, ARR_SIZE);
  127. buildMaxHeap(a, ARR_SIZE);
  128. cout << " The heap: " << endl;
  129. printArray(a, 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: \n";
  137. printArray(b, ARR_SIZE);
  138. cout << " Unsorted heap: \n";
  139. buildHeap_BottomUp(b, ARR_SIZE);
  140. printArray(b, ARR_SIZE);
  141. cout << " Sorted heap: \n";
  142. heapSort_BU(b, ARR_SIZE);
  143. printArray(b, ARR_SIZE);
  144.  
  145. return 0;
  146. }
  147.  
  148. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  149. // Debug program: F5 or Debug > Start Debugging menu
  150.  
  151. // Tips for Getting Started:
  152. // 1. Use the Solution Explorer window to add/manage files
  153. // 2. Use the Team Explorer window to connect to source control
  154. // 3. Use the Output window to see build output and other messages
  155. // 4. Use the Error List window to view errors
  156. // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  157. // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement