You are on page 1of 2

hackingzo ne s.

co m

http://hackingzo nes.co m/?p=1613

Heap Sort program in c


Heap sort there header f ile it contains f ollowing f unction void swap(int *x,int *y),void percdown(int A[],int i,int n),void display(int A[],int n),void heapsort(int A[],int n) by using these f ucniton and data variable it will perf orm Heap sort algorithm. Data structure Lab Source code Programming algorithm CS1152 c/c++ Heap.h file Heap Sort Programming Algorithm. #include #include #def ine lef tchild(i) (2*i+1) void swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; } void percdown(int A[],int i,int n) { int child,tmp; f or(tmp=A[i];lef tchild(i) { child=lef tchild(i); if (child!=n-1&&A[child+1]>A[child]) child++; if (tmp A[i]=A[child]; else break; } A[i]=tmp; } void display(int A[],int n) { int i; f or(i=0;i { printf (\t%d\t,A[i]); } printf (\n); } void heapsort(int A[],int n) { int i; f or(i=n/2;i>=0;i) percdown(A,i,n); display(A,n); f or(i=n-1;i>0;i) { swap(&A[0],&A[i]); percdown(A,0,i);

display(A,n); getch(); } }Heap.c file Heap Sort Algorithm Data Structure #includeheap.h void main() { int A[20],n; int i; clrscr(); printf (\n\n How many numbers do u want to sort:\t); scanf (%d,&n); printf (\n Enter the values); f or(i=0;i { printf (\nA[%d]\t,i); scanf (%d,&A[i]); } heapsort(A,n); getch(); } Heap Sort OUT PUT How many numbers do u want to sort: 5 Enter the values A[0] 100 A[1] 25 A[2] 10 A[3] 9 A[4] 5 100 25 10 9 5 25 9 10 5 100 10 9 5 25 100 9 5 10 25 100 5 9 10 25 100

Relat ed Post
Postf ix Program in c QUICK SORT program in C Enter your email address:

Delivered by FeedBurner

Post Footer automatically generated by Add Post Footer Plugin f or wordpress.

You might also like