Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- void swap(int *a, int *b) {
- int tmp = *a;
- *a = *b;
- *b = tmp;
- }
- void siftUp(int *heap, int i) {
- while (i > 0) {
- swap(&heap[i], &heap[(i - 1) / 2]);
- i = (i - 1) / 2;
- }
- }
- int main() {
- std::ifstream fin("heapsort.in");
- std::ofstream fout("heapsort.out");
- int n;
- fin >> n;
- int heap[n];
- int size = 0;
- int index = 0;
- for (int i = 1; i <= n; ++i) {
- heap[index++] = i;
- size++;
- }
- for (int i = 1; i < n; ++i) {
- int tmp = i;
- swap(&heap[0], &heap[i]);
- if (i == n - 1)
- break;
- siftUp(heap, tmp);
- }
- for (int i = 0; i < size; ++i) {
- fout << heap[i] << " ";
- }
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment