1: #define leftchild(n)(2*n+1)
2: void swap(int *a,int *b)
3: {
4: int temp=*a;
5: *a=*b;
6: *b=temp;
7: }
8: void heapify(int a[],int i,int n)
9: {
10: int temp,maxchild;
11: for(temp=a[i];leftchild(i)<n;i=maxchild)
12: {
13: maxchild=leftchild(i);
14: if(maxchild!=n-1 && a[maxchild]<a[maxchild+1])
15: maxchild++;
16: if(temp<a[maxchild])
17: a[i]=a[maxchild];
18: else
19: break;
20: }
21: a[i]=temp;
22: }
23: void heapsort(int a[],int n)
24: {
25: int i;
26: for(i=n/2;i>=0;i--)
27: heapify(a,i,n);
28: for(i=n-1;i>0;i--)
29: {
30: swap(&a[0],&a[i]);
31: heapify(a,0,i);
32: }
33: }
34: int main()
35: {
36: int a[]={1,245,7,2,0,-1,5,43,23,60};
37: int i,n=10;
38: heapsort(a,n);
39: for(i=0;i<n;i++)
40: {
41: printf("%d ",a[i]);
42: }
43: getch();
44: }
This blog will cover all important aspects of 'C' 'C++', 'Data Structures in C' and other Technical stuffs. In this blog you will find good C Interview Questions Answers. I will be posting both multiple choice and subjective type C interview questions and answers. Tutorials will be posted from time to time that will focus on problem solving.
Sunday, October 2, 2011
Heap Sort C program for DEV C++
Labels:
sorting
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment