Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Saeideh Shah   on May 26 In Java Category.

  
Question Answered By: Jignesh Bodarya   on May 26

/*Devolop by:SRIMCA */
import java.io.*;

class heap
{
void constructheap(int a[],int n)
{
int ci,pi,temp,i;
for(i=0;i<n;i++)
{
ci=i;
do
{
pi=(ci-1)/2;
if(a[pi]<a[ci])
{
temp=a[pi];
a[pi]=a[ci];
a[ci]=temp;
}
ci=pi;
}while(ci!=0);
}
}
void sort(int a[],int n)
{
int temp,j,k=1;
for(j=n-1;j>=0;j--,k++)
{
temp=a[0];
a[0]=a[n-k];
a[n-k]=temp;

constructheap(a,n-k);
}
}
void display(int a[],int n)
{
int j;
for(j=0;j<n;j++)
System.out.println(a[j]);
}
void input(int a[],int n)throws IOException
{
int i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(i=0;i<n;i++)
{
System.out.print("Enter Element:");
a[i]=Integer.parseInt(br.readLine());
}
}
public static void main(String args[])throws IOException
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter N:");
n=Integer.parseInt(br.readLine());
int a[]=new int[n];
heap h=new heap();
h.input(a,n);
h.constructheap(a,n);
h.sort(a,n);
h.display(a,n);
}
}

Share: