My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
|
sort list of elements using heap sort.
Posted Date: 03 Jun 2008 Resource Type: Articles/Knowledge Sharing Category: Education
|
Posted By: Vidya Member Level: Diamond Rating: Points: 1
|
|
|
|
Program to sort list of elements using heap sort.
#include #include #define SIZE 20
/**************Function Declaration Begin**********/ void get_elements(int A[],int n); void movedown(int pos,int A[],int n); void Heap_sort(int A[],int n); void show_elements(int A[],int n); /**************Function Declaration End**********/
void main() { int n,A[SIZE]; clrscr(); printf(“\n\t\t Program for Merge sort:”); printf(“\n How many numbers you want to store in the array:”); scanf(“%d”,&n); get_elements(A,n); Heap_sort(A,n); show_elements(A,n); getch(); }
/********** heapify & adjusting element position **********/ /********** Function Definition begins **********/ void movedown(int pos,int A[],int n) { int k,r,l,max,temp; for(k=pos;;) { l=2*k+1; r=l+1; if(l>=n) { return; } else if(r>=n) { max=l; } else if(A[l]>A[r]) { max=l; } else { max=r; } if(A[k]>A[max]) { return; } temp=A[k]; A[k]=A[max]; A[max]=temp; k=max; } } /********** Function Definition ends **********/
/******************* heap sorting technique *****************/ /********** Function Definition begins **********/ void Heap_sort(int A[],int n) { int i,temp; for(i=n/2;i>=0; —i) /* Performimg Heapify */ { movedown(i,A,n); } for(i=n-1;i>0;i—) { temp=A[0]; A[0]=A[i]; A[i]=temp; movedown(0,A,i); } } /********** Function Definition ends **********/
/********** inputting elements **********/ /********** Function Definition begins **********/ void get_elements( int A[],int n) { int i; printf(“\n Enter %d elemets:\n”,n); for (i=0;i scanf(“%d”,&A[i]); printf(“\n Array before sorting: “); for(i=0;i printf(“%d “,A[i]); printf(“\n”); } /********** Function Definition ends **********/
/********** displaying elements **********/ /********** Function Definition begins **********/ void show_elements(int A[],int n) { int i; printf(“\n Array after sorting: “); for(i=0;i { printf(“%d “,A[i]); } printf(“\n”); } /********** Function Definition ends **********/
? OUTPUT Program for Heap sort: How many numbers you want to store in the array:6 Enter 6 elemets: 66 44 55 22 33 11 Array before sorting: 66 44 55 22 33 11 Array after sorting: 11 22 33 44 55 66
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|
Watch TV Channels
Watch Asianet TV onlineKairali TV in InternetSurya TV onlineAmritha TV Channel
|