Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stack>
- #include<math.h>
- #include<iostream>
- #include<algorithm>
- #include<string.h>
- #include<string>
- #include<set>
- #include<memory.h>
- #include<vector>
- #include<map>
- #include<queue>
- #include<iomanip>
- #include<ctime>
- #define forn(i, n) for (int i = 0; i < (n); i++)
- using namespace std;
- inline int myRand() {
- return (rand() << 16) ^ rand();
- }
- const int N = 1000 * 10;
- int h[N];
- int n = 0;
- void heapPush (int x) {
- h[n++] = x;
- int i = n - 1;
- while (i > 0 && h[(i - 1) / 2] < h[i])
- swap(h[i], h[(i - 1) / 2]), i = (i - 1) / 2;
- }
- void heapify(int x) {
- while (2 * x + 1 < n) {
- int j = 2 * x + 1;
- if (j + 1 < n && h[j + 1] > h[j])
- j++;
- if (h[x] >= h[j])
- break;
- swap(h[x], h[j]);
- x = j;
- }
- }
- void heapSort () {
- while (n > 1) {
- heapify(0);
- swap(h[0], h[n - 1]);
- n--;
- }
- }
- int main() {
- srand(time(NULL));
- forn (i, N) {
- int tek = myRand() % N;
- heapPush((tek < 0 ? -tek : tek));
- }
- heapSort();
- forn (i, N) {
- cout << h[i] << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement