Saturday, June 16, 2012

Very Important Sorting Algorithms

Selection  Sort
Insertion Sort
Shell Sort
Merge Sort
Radix sort

Selection  Sort
      i)Declare and initialize necessary variables such as array [ ] , i, j , large, n .
      ii)For ( i=n-1 ; i > 0 ; i -- )  repeat following steps
               large=x[ 0 ]
               index=0
              ii.a  For ( j=1;j<=i ; j++)
                 IF (x[ j ]>large)
                         large=x[ j ]
                         index=j
             ii.b x[index] =x[ i ]
                          x[ i ]=large
      iii)Display the sorted array'

Insertion Sort
    i) i)Declare and initialize necessary variables such as array [ ] , i, j , large, n 
    ii)Insert each x[ ] into sorted file i.e.
           for  k=1 to k<n ,repeat
                temp=x[ k ]
                ii.a Move down one position all elements greater than temp i.e.
                      for ( i=k-1 ; j>=0  && temp<x [ i ] ; i ++)
                             x[i+1]=x[ i ]
                 ii.b   x[i+1]=temp
     iii)Display the sorted array
note:: in above algorithm we take x[0] as sorted file initially.



Shell Sort
   Shell sort is generalization of insertion sort and devised by Donald Shell in 1959.
This method sorts separate sub files of original file i.e. Divide the original file into smaller sub files.
Then sort individual sub files using any sorting algorithms.We choose increment 'K' for dividing the original file into smaller sub files.And the process is repeated until ' K' becomes 1.

Merge Sort
 Merge sort based on the divide and conquer paradism.
Let A[p.......r] is given array with indices p=1 to r=n then
   i)Divide step
       if a given array A has zero or one element then simply return,it is already sorted.
       else split  A[p........r] inot two sub arrays as A[p..........q] and A[q+1...........r] each containing  
        about the half of the elements.
 ii)Conquer step
        conquer by recursively sorting the two sub arrays A[p..........q] and A[q+1..........r]
iii)Combine step
        combine the elements back in A[p......r] by merging the two sorted sub arrays into the sorted sequence

 
Radix sort
    i)Declare and initialize necessary variables
   ii)For( k=least significant digit ; k<=most significant digit ; k++)
        { 
               for(i=0; i<n; i++)
                   {
                       y=x[i]
                        i=k-th digit of y
                   }

               for(qu=0; qu<10; qu++)
                  {
                    place elements of queue[qu] in next sequential position of x
                  }
        }

No comments:

Post a Comment