Heap Sort

C++ Code


/* program to heap sort array elements */
#include<iostream>
#include<math.h>
using namespace std;
int b[100],ptr=0,tn;
//function to form heap of a given tree
void heapify(int n,int a[])
{
     //if no node left
     if(n==0)
     {
            return;
     }
     int l,r,tmp,curr;
     //decrease current node number
     curr=n-1;
     l=(curr+1)*2-1;
     r=l+1;
     //if parent if less then left child
     if(a[curr]<a[l]&&r!=tn)
     {
         //if left child is less then right child
            if(a[l]<a[r])
            {
                //swap parent with right child
                 tmp=a[curr];
                 a[curr]=a[r];
                 a[r]=tmp;
            }
            else
            {
                //swap parent with left child
                tmp=a[curr];
                a[curr]=a[l];
                a[l]=tmp;
            }
     }
     else
     if(r!=tn)
     {
         if(a[curr]<a[r])
         {
             tmp=a[curr];
             a[curr]=a[r];
             a[r]=tmp;
         }
     }
     heapify(n-1,a);

}
//function to heap sort the numbers in array
void heapsort(int n,int a[])
{
     int t=n;
     //loop till all elements are sorted
     for(int i=0;i<t;i++)
     {
          heapify(n/2,a);
          b[ptr]=a[0];
          a[0]=a[n-1];
          ptr++;
          n--;
          tn=n;
     }
}
//start of main function
int main()
{
    int a[100],n;
    cin>>n;
    tn=n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    heapsort(n,a);
    cout<<"Sorted Array is";
    for(int i=0;i<n;i++)
    {
           cout<<" "<<b[i];
    }
    return 0;
}


JAVA Code

import java.io.*;
class heapsort{
	int n;
	int heap[];
	 heapsort(int i){
		n=i;
		heap=new int[i+1];
	}
	void downheap(int start,int finish)
	{
		int i=start,l,r,max,temp;
		l=2*start;
		r=l+1;
		if(l<=finish)
		{
			max=heap[l];
			i=l;
			if(r<=finish)
			if(heap[r]>max)
			{
				max=heap[r];
				i=r;
			}
		}
		if(heap[start]<heap[i])
		{
			temp=heap[start];
			heap[start]=heap[i];
			heap[i]=temp;
			downheap(i,finish);
		}
	}
	void upheap(int start)
	{
		int temp,par;
		if(start>1)
		{
			par=start/2;
			if(heap[par]<heap[start])
			{
				temp=heap[start];
				heap[start]=heap[par];
				heap[par]=temp;
				upheap(par);
			}
		}
	}
	void insert(int n,int item)
	{
		n++;
		heap[n]=item;
		upheap(n);
	}
	void heapify(int n)
	{
		int i,index;
		index=n/2;
		for(i=index;i>=1;i--)
			downheap(i,n);
	}

	int Delete()		
	{
		int temp;
		temp=heap[1];
		heap[1]=heap[n];
		n--;
		downheap(1,n);
		return temp;
	}
}
class heapsortmethod
{
	public static void main(String args[]) throws Exception
	{
		int temp,n,i,j,k;
		           BufferedReader cin= new BufferedReader (new InputStreamReader(System.in));
		System.out.println("enter the no. of elements");
		n=Integer.parseInt(cin.readLine());
		heapsort arr = new heapsort(n);
		System.out.println("enter the integers to be sorted");
		for(i=1;i<=n;i++)
		{
			arr.heap[i]= Integer.parseInt(cin.readLine());
		}
		arr.heapify(n);
		System.out.println("the elements indescending order are:");
		for(i=1;i<=n;i++)
			System.out.println(arr.Delete());
	}
}