Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <string>
- #pragma warning(disable:4996)
- using namespace std;
- int n, amount;
- int *arr;
- void MSwap(int a, int b) {
- int c;
- c = arr[a];
- arr[a] = arr[b];
- arr[b] = c;
- }
- void Heapify(int n, int i) {
- int left_child = 2 * i + 1;
- int right_child = 2 * i + 2;
- int max = i;
- if (left_child < n && arr[left_child] > arr[max])
- max = left_child;
- else
- max = i;
- if (right_child < n && arr[right_child] >= arr[max])
- max = right_child;
- if (max != i) {
- MSwap(i, max);
- Heapify(n, max);
- }
- }
- void HeapSort() {
- for (int i = (n - 1) / 2; i >= 0; i--) {
- Heapify(n, i);
- }
- for (int i = n - 1; i > 0; i--) {
- MSwap(i, 0);
- n--;
- Heapify(n, 0);
- }
- }
- int main() {
- cin >> amount;
- arr = new int[amount];
- for (int i = 0; i < amount; ++i) {
- cin >> arr[i];
- n++;
- }
- HeapSort();
- //cout << "Amount: " << amount << " Sort:" << endl;
- /*for (int i = 0; i < amount; ++i) {
- cout << arr[i] << " ";
- }*/
- //cout << endl;
- if (amount % 2 == 0) {
- cout << double(arr[(amount-1) / 2] + arr[(amount-1) / 2 + 1]) / 2 << endl;
- }
- else {
- cout << int(arr[(amount) / 2]) << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement