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

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.
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.
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.
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.
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.

Total Pageviews

Followers


Labels

Popular Posts

free counters