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 | 1 comments
Sunday, July 29, 2012
It is not allowed since static nature of the method will give permission to access it directly by class name only. But contrarily abstract nature will prohibit it from doing so. This results in a conflicting nature (based on meaning of two modifiers) of the method. Due to this reason Java does not allow you to declare a method both static and abstract. If you try to do this you will get a compile error as illegal combination of modifiers : abstract and static.
Labels:Java FAQs | 0
comments
Saturday, July 28, 2012
An abstract class is one which cannot be instantiated. Its properties and characteristics cannot be used until and unless it is not inherited by a concrete class. So in order to use such type of classes you will have to extend that class and override its methods.
A final class is one that cannot be inherited i.e. a class defined as final cannot be extended and redefined using the software reusability concept of inheritance.
Now if you try to combine both of these two things, then the class declared final and abstract cannot be inherited as being declared final and since it is abstract we cannot use it directly by creating its object. Hence such a class is of no use and makes no sense at all. For this reason only it is not possible to declare a class both final and abstract.
A final class is one that cannot be inherited i.e. a class defined as final cannot be extended and redefined using the software reusability concept of inheritance.
Now if you try to combine both of these two things, then the class declared final and abstract cannot be inherited as being declared final and since it is abstract we cannot use it directly by creating its object. Hence such a class is of no use and makes no sense at all. For this reason only it is not possible to declare a class both final and abstract.
Labels:Java FAQs | 0
comments
Friday, July 27, 2012
A method is a function that is written in a class. We do not have functions in java; instead we have methods. This means whenever a function is written in java,it should be written inside the class only. But if we take C++, we can write the functions inside as well as outside the class . So in C++, they are called member functions and not methods.
Labels:Java FAQs | 0
comments
As you know C/C++ supports pointers. But Java doesn't. This is because of the following reasons :
1) Pointers can result in a crash of your program. So it helps in stabilizing yor code in Java without pointers.
2) Pointers can cause programmers to become confused.
3) Java was developed to make system more secure. So keeping security in mind it was decided to omit pointers as pointers can be used very efficiently to create VIRUS/Malware programs.
1) Pointers can result in a crash of your program. So it helps in stabilizing yor code in Java without pointers.
2) Pointers can cause programmers to become confused.
3) Java was developed to make system more secure. So keeping security in mind it was decided to omit pointers as pointers can be used very efficiently to create VIRUS/Malware programs.
Labels:Java FAQs | 0
comments
In Java program execution starts from main method. And the main method should look like this public static void main(String[] args). Here it is declared static. This is due to following reasons :
1) It can be called directly by the class name without creating its instance.
2) If the class containing main have more than one constructor then JVM won't be able to understand which constructor to use while creating the object.
3) Again if there is a parameterized constructor in the class containing main then also JVM won't understand what value to pass while creating the object.
1) It can be called directly by the class name without creating its instance.
2) If the class containing main have more than one constructor then JVM won't be able to understand which constructor to use while creating the object.
3) Again if there is a parameterized constructor in the class containing main then also JVM won't understand what value to pass while creating the object.
Labels:Java FAQs | 0
comments
Thanks to all of you who have supported me in past. Keeping your request I have opened this blog to meet your needs. Hope you will like it and give important suggestions to carry on with my work
Subscribe to:
Posts
(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
)