Monday, July 30, 2012
Radix Sort is a type of bucket sort. This type of sort is based on radix i.e. 10 for decimal numbers and 26 for alphabets. This technique is very useful in sorting words alphabetically. Arrays are sorted in a total of "d" passes where "d" is the number of digits of highest number..
For decimal numbers : In first pass array is sorted according to units digit , then in next pass sorted according to tens digit and so on.This process goes on till all the digits of highest numbers are traversed.
For words : In first pass array is sorted according to first letter , and in second pass second character is considered and so on.
Here is the code for you -->
import java.io.*;
public class Radix_Sort {
public void sort(int a[],int rad,int max){
int tmp[][]=new int[a.length][10];
for(int i=0;i<max;i++){
int c=0;
for (int j=0;j<a.length;j++)
for(int k=0;k<rad;k++)
tmp[j][k]=0;
for(int k=0;k<a.length;k++){
int d=(int)((a[k]/Math.pow(10,i))%10);
tmp[k][d]=a[k];
}
for (int j=0;j<rad;j++)
for(int k=0;k<a.length;k++)
if(tmp[k][j]!=0)
a[c++]=tmp[k][j];
}
disp(a);
}
public void input()throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter array size : ");
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];
System.out.println("Enter elements in array ------->");
int max=0;
for (int i = 0; i<n; i++) {
System.out.print("Enter number : ");
a[i]=Integer.parseInt(br.readLine());
if(a[i]>max)
max=a[i];
}
sort(a,10,digit_count(max));
}
int digit_count(int n){
int d=0;
while (n!=0) {
d++;
n/=10;
}
return d;
}
public void disp(int a[]){
System.out.println("\nArray after sorting -------->");
for (int i = 0; i<a.length; i++)
System.out.print(a[i]+" ");
}
public static void main(String[] args) throws Exception{
new Radix_Sort().input();
}
}
You can also download the source from below links :
DOWNLOAD source JAVA file from MediaFire
DOWNLOAD the JAVA file from 4shared
For decimal numbers : In first pass array is sorted according to units digit , then in next pass sorted according to tens digit and so on.This process goes on till all the digits of highest numbers are traversed.
For words : In first pass array is sorted according to first letter , and in second pass second character is considered and so on.
Here is the code for you -->
import java.io.*;
public class Radix_Sort {
public void sort(int a[],int rad,int max){
int tmp[][]=new int[a.length][10];
for(int i=0;i<max;i++){
int c=0;
for (int j=0;j<a.length;j++)
for(int k=0;k<rad;k++)
tmp[j][k]=0;
for(int k=0;k<a.length;k++){
int d=(int)((a[k]/Math.pow(10,i))%10);
tmp[k][d]=a[k];
}
for (int j=0;j<rad;j++)
for(int k=0;k<a.length;k++)
if(tmp[k][j]!=0)
a[c++]=tmp[k][j];
}
disp(a);
}
public void input()throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter array size : ");
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];
System.out.println("Enter elements in array ------->");
int max=0;
for (int i = 0; i<n; i++) {
System.out.print("Enter number : ");
a[i]=Integer.parseInt(br.readLine());
if(a[i]>max)
max=a[i];
}
sort(a,10,digit_count(max));
}
int digit_count(int n){
int d=0;
while (n!=0) {
d++;
n/=10;
}
return d;
}
public void disp(int a[]){
System.out.println("\nArray after sorting -------->");
for (int i = 0; i<a.length; i++)
System.out.print(a[i]+" ");
}
public static void main(String[] args) throws Exception{
new Radix_Sort().input();
}
}
You can also download the source from below links :
DOWNLOAD source JAVA file from MediaFire
DOWNLOAD the JAVA file from 4shared
Labels:Sorting
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
Links
Blog Archive
-
▼
2012
(
25
)
-
▼
July
(
7
)
- Java program to perform Radix Sort
- Why is static abstract method not allowed in Java ?
- Why is final abstract class not possible in Java
- What is the difference between a function and a me...
- Why the concept of pointers is omitted in Java ?
- Why is main method static in Java ?
- Thanks to all of you who have supported me in past...
-
▼
July
(
7
)
this code is for digit i ask for string and words if u can help me ,,?
ReplyDelete