Advertisement
Guest User

1306

a guest
Mar 30th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. #pragma warning(disable:4996)
  5.  
  6. using namespace std;
  7.  
  8. int n, amount;
  9. int *arr;
  10.  
  11. void MSwap(int a, int b) {
  12. int c;
  13. c = arr[a];
  14. arr[a] = arr[b];
  15. arr[b] = c;
  16. }
  17.  
  18. void Heapify(int n, int i) {
  19. int left_child = 2 * i + 1;
  20. int right_child = 2 * i + 2;
  21. int max = i;
  22. if (left_child < n && arr[left_child] > arr[max])
  23. max = left_child;
  24. else
  25. max = i;
  26. if (right_child < n && arr[right_child] >= arr[max])
  27. max = right_child;
  28. if (max != i) {
  29. MSwap(i, max);
  30. Heapify(n, max);
  31. }
  32. }
  33.  
  34. void HeapSort() {
  35. for (int i = (n - 1) / 2; i >= 0; i--) {
  36. Heapify(n, i);
  37. }
  38. for (int i = n - 1; i > 0; i--) {
  39. MSwap(i, 0);
  40. n--;
  41. Heapify(n, 0);
  42. }
  43. }
  44.  
  45. int main() {
  46. cin >> amount;
  47. arr = new int[amount];
  48. for (int i = 0; i < amount; ++i) {
  49. cin >> arr[i];
  50. n++;
  51. }
  52. HeapSort();
  53. //cout << "Amount: " << amount << " Sort:" << endl;
  54. /*for (int i = 0; i < amount; ++i) {
  55. cout << arr[i] << " ";
  56. }*/
  57. //cout << endl;
  58. if (amount % 2 == 0) {
  59. cout << double(arr[(amount-1) / 2] + arr[(amount-1) / 2 + 1]) / 2 << endl;
  60. }
  61. else {
  62. cout << int(arr[(amount) / 2]) << endl;
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement