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();
       }
   }





Friday, June 5, 2015

Array - XII [ Shell Sort ]



Shell Sort Shellsorts largely a deviation of Insertion Sort, it is also one of the oldest sorting algorithms. Shellsort is named after its inventor Donald Shell. It is fast, easy to understand and easy to implement. In Insertion sort an elements only move by one position in front. When an element has to be moved far-off, it requires to move many times. ShellSort allows swapping of far items. Its complexity is a little more complicated. The running time of Shellsort is greatly dependent on the gap sequence it uses.

Complexity :


Worst case performance              O(n^2)
Best case performance                O(N)
Average case performance           depends on gap sequence


Algorithm.


function shellsort(int arr[],int n)
           int last,tmp,limit,temp,k
           int mid=n/2
           while mid>0
              do
                   last=0;
                   limit=n-mid;
                   while last==0
                        do
                            tmp=0;
                            for k=0 to limit-1 step 1
                                do
                                      if arr[k]>arr[k+mid]
                                                then
                                                          temp=arr[k]
                                                          arr[k]=arr[k+mid]
                                                          arr[k+mid]=temp
                                                          tmp=k
                                                endif
                                endfor
                            limit=tmp-mid
                            if tmp==0 then
                                then
                                      last=1
                                endif
                        enddo
                   mid=mid/2
              enddo
      end



Source Code



import java.io.*;

class Shell_Sort
{
  void shellsort(int Arr[],int n)
      {
         int i, k, j,mid, temp ;
         for (mid = n / 2; mid > 0; mid /= 2)
            {
                for (i = mid; i < n; i++)
                    {
                         temp = Arr[i];
                         for (j = i; j >= mid; j -= mid)
                             {
                                 if (temp < Arr[j - mid])
                                     {
                                          Arr[j] = Arr[j - mid];
                                     }
                                 else
                                     {
                                          break;
                                     }
                               }
                         Arr[j] = temp;
                    }
             }
      }

  void Display(int Arr[],int n)
     {
          int i;
          for(i=0;i<n ;i++)
              {
                   System.out.print(Arr[i]+"  ");
              }
     }
  public static void main(String args[])throws IOException
      {
           BufferedReader Br = new BufferedReader(new InputStreamReader(System.in));
           int i;
           Shell_Sort S=new Shell_Sort();
           int Arr[],n;
           System.out.print("\n\tEnter Size of Array below 20 : ");
           n=Integer.parseInt(Br.readLine());
           Arr=new int[n];
           for(i=0;i<n; i++)
              {
                   System.out.print("\n\tEnter Array Element at  : "+i+" -> ");
                   Arr[i]=Integer.parseInt(Br.readLine());;
              }
           System.out.print("\n\tOriginal Array :  ");
           S.Display(Arr,n);
           S.shellsort(Arr,n);
           System.out.print("\n\tAfter Sort     :  ");
           S.Display(Arr,n);
      }
    }