Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1.  
  2. //Se considera urmatoarea secventa de valori:
  3. //
  4. //17 34 51 68 85 2 19 36 53 70 87 4 21 38 55 72 89 6 23 40 57 74 91 8 25 42 59 76 93 10 27 44
  5. //61 78 95 12 29 46 63 80 97 14 31 48 65 82 16 33 50 67 84 1 18 35 52 69 86 3 20 37 54 71 88 5
  6. //22 39 56 73 90 7 24 41 58 75 92 9 26 43 60 77 94 11 28 45 62 79 96 13 30 47 64 81 15 32 49 83
  7. //
  8. //
  9. //
  10. //------------------------------------
  11. //A. Restantieri seria 2019-2020
  12. //------------------------------------
  13. //
  14. //Sa se construiasca o structura max-heap cu urmatoarele valori:
  15. //
  16. //1) afisati primele 5 valori ale structurii de date heap
  17. //
  18. //2) afisati toate elementele de pe nivelul 3 (radacina este nivelul 0)
  19. //
  20. //3) Sortati aceste elemente cu algoritmul HeapSort
  21.  
  22.  
  23. // C++ program for implementation of Heap Sort
  24. #include <iostream>
  25.  
  26. using namespace std;
  27.  
  28. // To heapify a subtree rooted with node i which is
  29. // an index in arr[]. n is size of heap
  30. void heapify(int arr[], int n, int i) {
  31. int largest = i; // Initialize largest as root
  32. int l = 2 * i + 1; // left = 2*i + 1
  33. int r = 2 * i + 2; // right = 2*i + 2
  34.  
  35. // If left child is larger than root
  36. if (l < n && arr[l] > arr[largest])
  37. largest = l;
  38.  
  39. // If right child is larger than largest so far
  40. if (r < n && arr[r] > arr[largest])
  41. largest = r;
  42.  
  43. // If largest is not root
  44. if (largest != i) {
  45. swap(arr[i], arr[largest]);
  46.  
  47. // Recursively heapify the affected sub-tree
  48. heapify(arr, n, largest);
  49. }
  50. }
  51.  
  52. // main function to do heap sort
  53. void heapSort(int arr[], int n) {
  54. // Build heap (rearrange array)
  55. for (int i = n / 2 - 1; i >= 0; i--)
  56. heapify(arr, n, i);
  57.  
  58. // One by one extract an element from heap
  59. for (int i = n - 1; i >= 0; i--) {
  60. // Move current root to end
  61. swap(arr[0], arr[i]);
  62.  
  63. // call max heapify on the reduced heap
  64. heapify(arr, i, 0);
  65. }
  66. }
  67.  
  68. /* A utility function to print array of size n */
  69. void printArray(int arr[], int n) {
  70. for (int i = 0; i < n; ++i)
  71. cout << arr[i] << " ";
  72. cout << "\n";
  73. }
  74.  
  75. int main() {
  76. int arr[] = {17, 34, 51, 68, 85, 2, 19, 36, 53, 70, 87, 4, 21, 38, 55, 72, 89, 6, 23, 40, 57, 74, 91, 8, 25, 42, 59,
  77. 76, 93, 10, 27, 44,
  78. 61, 78, 95, 12, 29, 46, 63, 80, 97, 14, 31, 48, 65, 82, 16, 33, 50, 67, 84, 1, 18, 35, 52, 69, 86, 3,
  79. 20, 37, 54, 71, 88, 5,
  80. 22, 39, 56, 73, 90, 7, 24, 41, 58, 75, 92, 9, 26, 43, 60, 77, 94, 11, 28, 45, 62, 79, 96, 13, 30, 47,
  81. 64, 81, 15, 32, 49, 83};
  82. int n = sizeof(arr) / sizeof(arr[0]);
  83.  
  84. cout << "n=" << n << "\n";
  85.  
  86. for (int i = n / 2 - 1; i >= 0; i--) {
  87. heapify(arr, n, i);
  88. }
  89. cout<<"Max-heap: ";
  90. printArray(arr, n);
  91. cout<<endl;
  92. cout << "Primele 5 elemente: ";
  93. printArray(arr, 5);
  94. cout << endl;
  95. cout << "Elementele de pe al 3-lea nivel sunt: ";
  96. for (int i = 7; i <= 14; i++)cout << arr[i] << " ";
  97. cout<<endl;
  98. heapSort(arr, n);
  99. cout << "Max-heap sorted: ";
  100. printArray(arr, n);
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement