Tweet
import java.io.*;
public class radix
{
public void radix(int a[],int n)throws IOException
{
int i,j,k=1,l,temp,cnt;
int b[][]=new int[10][n];
int max=a[0],c=0;
for(i=0;i<n;i++)
if(a[i]>max)
max=a[i];
while(max%10!=0)
{
c++;
max/=10;
}
for(i=0;i<10;i++)
for(j=0;j<n;j++)
b[i][j]=-999;
for(l=0;l<c;l++)
{
for(j=0;j<n;j++)
{
temp=(a[j]/k)%10;
b[temp][j]=a[j];
}
k=k*10; cnt=0;
for(i=0;i<10;i++)
for(j=0;j<n;j++)
if(b[i][j]!=-999)
a[cnt++]=b[i][j];
for(i=0;i<10;i++)
for(j=0;j<n;j++)
b[i][j]=-999;
}
}
public void main(String args[])throws IOException
{
System.out.print("\nProgram to sort array using radix sort sort : \n");
int n,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter array capacity : ");
n=Integer.parseInt(br.readLine());
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=(int)(Math.random()*100);
System.out.print("\nArray before sorting : ");
for(i=0;i<n;i++)
System.out.print(a[i] + " ");
radix(a,n);
System.out.print("\nArray after sorting : ");
for(i=0;i<n;i++)
System.out.print(a[i] + " ");
}
}
Radix Sort in Java
Posted by
LAHAUL SETH
~
Radix Sort in Java
2012-01-19T17:43:00+05:30
LAHAUL SETH
Java
|
Sorting
|
Comments
Radix Sort in Java
2012-01-19T17:43:00+05:30
LAHAUL SETH
Java
|
Sorting
|