#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
void swap(int &a, int& b)
{
int temp;
temp=b;
b=a;
a=temp;
}
void MaxHeapfy(int arr[],int size,int root)
{
int child =2*root+1;
if(child<=size-1)
{
int rightchild=child+1;
if(rightchild<=size-1)
if(arr[rightchild]>arr[child])
child=rightchild;
if(arr[child]>arr[root])
{
swap(arr[root],arr[child]);
MaxHeapfy(arr,size,child);
}
}
}
void HeapSort(int arr[],int size)
{
for(int i=size-1;i>=0;i--)
MaxHeapfy(arr,size,i);
int last=size-1;
for(int i=1;i<=size;i++,last--)
{
swap(arr[0],arr[last]);
MaxHeapfy(arr,last,0);
}
}
int main()
{
const int size=10;
int array[size]={10,2,13,41,23,52,7,31,65,90};
HeapSort(array,size);
for(int i=0;i<size;i++)
cout<<i+1<<" item is :"<<array[i]<<endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
void swap(int &a, int& b)
{
int temp;
temp=b;
b=a;
a=temp;
}
void MaxHeapfy(int arr[],int size,int root)
{
int child =2*root+1;
if(child<=size-1)
{
int rightchild=child+1;
if(rightchild<=size-1)
if(arr[rightchild]>arr[child])
child=rightchild;
if(arr[child]>arr[root])
{
swap(arr[root],arr[child]);
MaxHeapfy(arr,size,child);
}
}
}
void HeapSort(int arr[],int size)
{
for(int i=size-1;i>=0;i--)
MaxHeapfy(arr,size,i);
int last=size-1;
for(int i=1;i<=size;i++,last--)
{
swap(arr[0],arr[last]);
MaxHeapfy(arr,last,0);
}
}
int main()
{
const int size=10;
int array[size]={10,2,13,41,23,52,7,31,65,90};
HeapSort(array,size);
for(int i=0;i<size;i++)
cout<<i+1<<" item is :"<<array[i]<<endl;
return 0;
}