Radix Sort (Contoh Program Java)

import java.io.BufferedInputStream;
import java.io.*;

/**
*
* @author L Wira Satria M
*/
public class radix {
static void radixsort (int a[], int d){
int bmat[][], c[], e=1;
int i, j, k, m, digit, row, col;

bmat = new int [a.length][10];
c    = new int [10];

for (m=1; m<=d; m++){
for (i=0; i<10; i++){
c[i]=-1;
}
for (i=0; i<a.length; i++){
digit = (a[i]/e)%10;
c[digit]++;
row   = c[digit];
col   = digit;
bmat[row][col]=a[i];
}
k=-1;
for (i=0; i<10; i++){
if(c[i]!=-1){
for (j=0; j<=c[i]; j++){
k++;
a[k]=bmat[j][i];
}
}
}
e=e*10;
}
}
public static void main(String[] args) throws Exception{
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Berapa banyak bilangan untuk di urutkan ?”);
int n =Integer.parseInt(inp.readLine());

int a[]=new int[n];
for (int i=0; i<n; i++){

System.out.println(“Inputkan Nilai “+(i+1)+” : “);
a[i]=Integer.parseInt(inp.readLine());
}

radixsort (a,3);
System.out.println(“\n”+”Array Terurut: “);

for (int i=0; i<n; i++){
System.out.print(a[i]+” “);
}
System.out.println();
}

Jika di run melalui netbean ini hasilnya

Java Runing Radix

source : http://www.codext.com/2013/09/radix-sort-program-in-java.html

Telkom University Creating The Future

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar