Created
March 12, 2009 10:15
-
-
Save anshumanatri/78001 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream.h> | |
#include<conio.h> | |
#include<stdio.h> | |
#include<stdlib.h> | |
int h[10],n; | |
void heap(int m); | |
void main() | |
{ | |
clrscr(); | |
int i,m; | |
cout<<"\n Enter the number of elements:\t";cin>>n;m=n; | |
cout<<"\n Enter the elements of the heap:\t"; | |
for(i=1;i<=n;i++) scanf("%d",&h[i]); | |
heap(n); | |
for(i=1;i<=m;i++) | |
{ | |
int temp=h[1]; | |
h[1]=h[n]; | |
h[n--]=temp; | |
heap(2); | |
} | |
cout<<"\n Element sorted using heap sort:\t"; | |
for(i=1;i<=m;i++) | |
cout<<h[i]<<" "; | |
getch(); | |
} | |
void heap(int m) | |
{ | |
int k,v,j,i; | |
bool flag; | |
for(i=m/2;i>=1;i--) | |
{ | |
k=i; | |
v=h[k]; | |
flag=false; | |
while(!flag && 2*k<=n) | |
{ | |
j=2*k; | |
if(j<n) | |
if(h[j]<h[j+1]) | |
j=j+1; | |
if(v>=h[j]) | |
flag=true; | |
else | |
{ | |
h[k]=h[j]; | |
k=j; | |
} | |
} | |
h[k]=v; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment