/* Program to demonstrate heap sort */ #include #include void heapsort(int x[ ], int n) ; void heapup(int heap[ ], int newnode) ; void heapdown(int heap[ ], int root, int last) ; void show(int x[ ], int n) ; void main() { int i, n, x[20] ; clrscr() ; printf("Enter the number of elements: ") ; scanf("%d",&n) ; printf("Enter the elements:\n") ; for(i=0 ; i0 ; i--) { /* Swapping */ temp=x[0] ; x[0]=x[i] ; x[i]=temp ; show(x,n) ; /* Applying rectification */ heapdown(x,0,i-1) ; show(x,n) ; } } void heapup(int heap[ ], int newnode) { int parent, temp ; if(newnode>0) { parent=(newnode-1)/2 ; if(heap[newnode]>heap[parent]) { temp=heap[parent] ; heap[parent]=heap[newnode] ; heap[newnode]=temp ; heapup(heap,parent) ; } } } void heapdown(int heap[ ], int root, int last) { int leftchild, rightchild, largerchild, temp ; leftchild=2*root+1 ; rightchild=2*root+2 ; /* Checking whether leftchild exists */ if(leftchild<=last) { /*Checking whether right child exists */ if(rightchild<=last) if(heap[leftchild]>heap[rightchild]) largerchild=leftchild ; else largerchild=rightchild; else largerchild=leftchild; if(heap[root]