Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

heap sort

  Asked By: Saeideh    Date: Mar 11    Category: Java    Views: 1516
  

To Whom It May Concern,

Would you please give the source code of java applet heap sort? I have the code of java Heap sort. I want to implement java applet heap sort.

Best Regards,
saeideh

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Jignesh Bodarya     Answered 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);
}
}

 
Didn't find what you were looking for? Find more on heap sort Or get search suggestion and latest updates.




Tagged: