-
Notifications
You must be signed in to change notification settings - Fork 0
/
heapsort.cpp
53 lines (48 loc) · 1.03 KB
/
heapsort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
#define LEFT(i) ((i+1)<<1) - 1
#define RIGHT(i) (i+1)<<1
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
void heapify(int A[], int i, int n) {
int largest, l, r;
l = LEFT(i);
r = RIGHT(i);
while(l < n) {
if(A[l] > A[i]) largest = l;
else largest = i;
if(r < n && A[r] > A[largest]) largest = r;
if(largest != i) {
swap(&A[i], &A[largest]);
i = largest;
l = LEFT(i);
r = RIGHT(i);
}
else break;
}
}
void maxHeap(int A[], int n) {
for(int i = n/2; i >= 0; i--) {
heapify(A, i, n);
}
}
void heapsort(int A[], int n) {
maxHeap(A, n);
for(int i = n - 1; i > 0; i--) {
swap(&A[i], &A[0]);
heapify(A, 0, i);
}
}
int main() {
int a[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
int n = sizeof(a)/sizeof(int);
heapsort(a, n);
for(int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}