#ifndef HEAP_SORT_H
#define HEAP_SROT_H
#include<iostream>
void maxHeap(int *arr,unsigned int Length);
void maxHeap(int *arr,unsigned int rootIndex,unsigned int heapSize);
void builMaxHeap( int *arr,unsigned int heapSize);
inline unsigned int right(unsigned int index);
inline unsigned int left(unsigned int index);
void heapSort(int *arr,int Length);
inline unsigned int left(unsigned int index){
return 2*index+1;
}
inline unsigned int right(unsigned int index){
return 2*index+2;
}
void maxHeap(int *arr,unsigned int rootIndex,unsigned int heapSize){
unsigned int leftIndex=left(rootIndex);
unsigned int rightIndex=right(rootIndex);
unsigned int largest;
if(arr[leftIndex]>arr[rootIndex]&&leftIndex<heapSize){
largest=leftIndex;
}else{
largest=rootIndex;
}
if(arr[rightIndex]>arr[largest]&&rightIndex<heapSize){
largest=rightIndex;
}
if(largest!=rootIndex){
int temp=arr[largest];
arr[largest]=arr[rootIndex];
arr[rootIndex]=temp;
maxHeap(arr,largest,heapSize);
}
}
void builMaxHeap( int *arr,unsigned int heapSize){
for(int i=heapSize/2-1; i>=0;--i){
maxHeap(arr,i,heapSize);
}
}
void heapSort(int *arr,int Length){
for(int size=Length-1;size>0;size--){
builMaxHeap(arr,size);
int temp=arr[size];
arr[size]=arr[0];
arr[0]=temp;
}
}
#endif