Wednesday, September 14, 2016

Generic Type Data in Java



So often we use Template class in C++ but Java too have Generic Data type.
Generic type data will accept any and all types for the type of arguments. Generic allows a function or class to work on many different data types without being rewritten for each one. Instead of writing multiple overloaded functions for the same purpose but with different data types we can use generic type and do the same with a single function.



Ex. 1


  class Numb<T>
    {

        T A,B;

        public Numb(T A,T B)
          {
              this.A = A;
              this.B = B;
          }
        void Disp()
          {
              System.out.print(A+"  "+B);
          }

        public static void main(String[] args)
          {
              Numb n1 = new Numb(10,20);
              Numb n2 = new Numb(10.5,20.5);
              Numb n3 = new Numb("First","Second");
              System.out.println("\nInteger : ");
              n1.Disp();
              System.out.println("\nDouble  : ");
              n2.Disp();
              System.out.println("\nString  : ");
              n3.Disp();
  
           }
    }


Output:


Integer :
10  20
Double  :
10.5  20.5
String  :
First  Second



Ex. 2


class MAX<T>
    {

        T A,B,C;
        public MAX(T A,T B)
          {
              this.A = A;
              this.B = B;
          }
        void Maximum()
          {
              System.out.print("A : "+A+"   B : "+B+" =  ");
              System.out.print(A.toString().compareTo(B.toString())>0?A:B);
          }

        public static void main(String[] args)
          {
              MAX m1 = new MAX(10,20);
              MAX m2 = new MAX(10.5,20.5);
              MAX m3 = new MAX("First","Second");
              System.out.println("\nInteger : Maximum of ");
              m1.Maximum();
              System.out.println("\nDouble : Maximum of ");
              m2.Maximum();
              System.out.println("\nString : Maximum of ");
              m3.Maximum();
          }
     }



Output:


Integer : Maximum of
A : 10   B : 20 =  20
Double : Maximum of
A : 10.5   B : 20.5 =  20.5
String : Maximum of
A : First   B : Second =  Second



Thursday, June 25, 2015

Array - XIII [ Bucket Sort ]





Bucket SortBucket sort algorithm works by dividing an array into a number of buckets and then sort the number in each bucket using different sorting algorithm. It has some similarities to radix-sort.

Bucket sort works as follows:

1.     Add the number of the array to the empty “buckets” depending number of digits.
2.     Sort each non-empty buckets.
3.  Pick the numbers from each buckets in order and put back the elements into the original array.


Complexity :

•         Worst case performance             O(n2)
•         Average case performance          O(n + k)
•         Worst case space complexity       O(n * k)



Algorithm.


function BucktSort(int Num[], int n)
          int max=min=Num[0];
for i=1 to n step 1
     do
                    if Num[i] > max
                         then
                                  max = Num[i];
                    else if    Num[i] < min
                         then
                                  min = Num[i];
                    endif
          endfor
int buck[] = new int[max-min+1];
for i = 0; to n step 1
              do
                   buck[Num[i]-min]++;
          endfor
          k = 0;
          for i = 0 to buck.length step 1 
              do
                  for  j = 0 to buck[i], step 1
                        do
                             Num[k++] = i+min;
                  endfor
          endfor
end


Source Code


import java.io.*;
public class Bucket_Sort
{
    int Num[],Size;
    void Input()
       {
          BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
             try
                {
                    System.out.print("\tEnter Size : ");
                    Size=Integer.parseInt(Br.readLine());
                    Num=new int[Size];
                    for( int i = 0; i < Size; i++ )    
                      {                          
                           System.out.print("\tEnter Element at ["+(i+1)+"] : " );
                           Num[i]=Integer.parseInt(Br.readLine());
                       }
                 }
             catch(Exception Ex){}
        }
    void BucketSort()
        {       
           int min,max;
           max=min=Num[0];
           for( int i = 1; i < Size; i++ )   
              {
                  if( Num[i] > max )
                    {
                      max = Num[i];
                    } 
                  else if( Num[i] < min )
                    {
                      min = Num[i];
                    } 
              } 
           int buck[] = new int[max-min+1];
           for( int i = 0; i < Size; i++ )
              {  
                 buck[Num[i]-min]++;
              }  
           int k = 0;                                 
           for( int i = 0; i < buck.length; i++ ) 
              {
                 for( int j = 0; j < buck[i]; j++ )
                    {
                        Num[k++] = i+min;                    
                    }
              }
       }
    void display()
        {
           for( int i = 0; i < Size; i++ )    
              {  
                  System.out.print(Num[i]+" ");
              }
           System.out.println();
        }     
    public static void main(String args[])
      {
           Bucket_Sort BST =new Bucket_Sort();
           BST.Input();
           System.out.print("\n\tOriginal Numrray : ");
           BST.display();
           BST.BucketSort ();
           System.out.print("\n\tSorted Numrray : ");
           BST.display();
       }
   }