0% found this document useful (0 votes)
32 views2 pages

Binary Heap

Uploaded by

vardhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

Binary Heap

Uploaded by

vardhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

/******************************************************************************

Online C++ Compiler.


Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
#define MAX 10
#include <iostream>

using namespace std;

class BinaryHeapADT
{
public:
void heapSort(int a[MAX], int n);
void buildHeap(int a[MAX],int,int);
};

void BinaryHeapADT::heapSort(int a[MAX], int n)


{
int i;
int temp,k,z=0;
for(i=(n/2);i>0;i--)
buildHeap(a,i,n);

for(i=n;i>=1;i--)
{
temp = a[1];
a[1] = a[i];
a[i] = temp;
buildHeap(a,1,i-1);
}
}

void BinaryHeapADT::buildHeap(int a[MAX],int root,int bottom)


{
int temp,maxc;
int done = 0;
while((root*2 <= bottom) && (!done))
{
if(root*2==bottom)
maxc = root*2;
else
if(a[root*2] > a[root*2+1])
maxc = root*2;
else
maxc = root*2+1;
if(a[root] < a[maxc])
{
temp = a[root];
a[root] = a[maxc];
a[maxc] = temp;
root = maxc;
}
else
done = 1;
}
}

int main()
{
int a[7]={0,33,66,22,88,55,44};
BinaryHeapADT b;
b.heapSort(a,7);
for(int i=1;i<=7;i++)
cout<<a[i]<<" ";

return 0;
}

You might also like