#include <iostream>
using namespace std;
class HeapSort {
public:
int* heapSort(int* A, int n) {
buildMaxHeap(A, n);
int tmp;
for(int i = n-1; i >0; i--){
tmp = A[0];
A[0] = A[i];
A[i] = tmp;
adjustMaxHeap(A, 0, i);
}
return A;
}
void adjustMaxHeap(int* A, int index_node, int size){
int var_max = index_node;
int left_child = index_node*2+1;
int right_child = index_node*2+2;
int tmp;
// cout<<"left_child: "<<left_child<<endl;
// cout<<"right_child: "<<right_child<<endl;
// cout<<"var_max: "<<var_max<<endl;
if(A[left_child] > A[var_max] && left_child < size){
var_max = left_child;
}
if(A[right_child] > A[var_max] && right_child < size){
var_max = right_child;
}
if(var_max != index_node){
tmp = A[index_node];
A[index_node] = A[var_max];
A[var_max] = tmp;
adjustMaxHeap(A, var_max, size);
}
}
void buildMaxHeap(int* A, int size){
for(int i = (size/2-1); i >= 0; i--){
// cout<<"i: "<<i<<endl;
adjustMaxHeap(A, i, size);
}
}
};
int main()
{
int a[6] = {1,5,2,3,2,3};
HeapSort sorter;
int* res = sorter.heapSort(a, 6);
cout<<"result: "<<endl;
for(int i = 0; i < 6; i++){
cout<<res[i]<<" ";
}
cout<<endl;
return 0;
}