Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- void swap(int *x, int *y)
- {
- int temp = *x;
- *x = *y;
- *y = temp;
- }
- void heapify(int* arr, int n, int i)
- {
- int maxi = i;
- int left = 2 * i + 1;
- int right = 2 * i + 2;
- if (left < n && arr[left] > arr[maxi])
- maxi = left;
- if (right < n && arr[right] > arr[maxi])
- maxi = right;
- if (maxi != i)
- {
- swap(&arr[i], &arr[maxi]);
- heapify(arr, n, maxi);
- }
- }
- void heapSort(int* arr, int n)
- {
- for (int i = n / 2 - 1; i >= 0; i--)
- heapify(arr, n, i);
- for (int i = n - 1; i >= 0; i--)
- {
- swap(&arr[0], &arr[i]);
- heapify(arr, i, 0);
- }
- }
- int main()
- {
- int n = 0;
- scanf("%d", &n);
- int *arr = (int *) malloc(sizeof(int) * n);
- for (int i = 0; i < n; i++)
- scanf("%d", &arr[i]);
- heapSort(arr, n);
- for (int i = 0; i < n; i++
- printf("%d ", arr[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement