Wednesday, September 29, 2010

(OOPS)Sorting Algorithm Using Templates

EX NO.                               SORTING ALGORITHM USING TEMPLATES

AIM: To create a c++ program to implement sorting algorithm using templates.

ALGORITHM:
Step 1:Start the program.
Step 2: Define the functions for bubble sort,insertion sort,selection sort and merge sort.
Step 3:Call the respective functions to perform the sorting operations.
Step 4:Display the results.
Step 5:Stop the program.

PROGRAM: 


#include<iostream.h>
#include<conio.h>
template<class T>
class sort
{
 public:
 T a[20];
 int n;
 sort()
  {
  }
 void display(T*a,int n)
  {
   for(int i=0;i<n;i++)
   {
    cout<<a[i]<<"\t";
   }
   cout<<endl;
  }
 void bubble(T*a,int n)
  {
   T tmp;
   for(int i=0;i<n-i;i++)
   {
    for(intj=i+1;j<n;j++)
    {
     if(a[i]>a[j])
     {
      tmp=a[i];
      a[i]=a[j];
      a[j]=tmp;
     }
    }
   }
  }
 void insertion(T*a,int n)
  {
   for(int p=1,p<n;p++)
   {
    T tmp=a[p];
    for(int j=p;j>0&&tmp<a[j-1];j--)
    a[j]=a[j-1]
    a[j]=tmp;
   }
  }
 void selection(T*a,int n)
  {
   T tmp,min;
   for(int i=0,i<n-1;i++)
   {
    min=i;
    for(int j=i+1;j<w,j++)
    {
     if(a[min]>a[j])
     min=j;
    }
    tmp=a[min];
    a[min]=a[i];
    a[i]=tmp;
   }
  }
 void merge(T*a,int n)
 {
  T.tmp array[20];
  merge(a,tmparray,0,n-1);
 }
 void merg(T*a,T*tmparray,int left,int right)
 {
  if(left<right)
  {
   int centre=(left+right)/2
   merge(a,tmparray,left,center);
   merge(a,tmparray,centre+1,right);
   {
    merge1(a,tmparray,left,center+1,right);
   }
  }

 void merge1(T*a,T*tmparray,int left pos,int right pos,int right end)
 {
  int left end=right pos-1;
  int tmppos=leftpos;
  int numelement=right end-left pos+1;
  while(leftpos<=leftend&&rightpos<=right end)
  {
   if(a[leftpos]<=a[right pos])
   tmparray[tmppos++]=a[leftpos++];
   else
   tmparray[tmpos++]=a[rightpos++];
  }
  while(leftpos<=leftend)
  tmparray[tmppos++]=a[leftpos++];
  while(rightpos<=rightend)
  tmparray[tmppos++]=a[rightpos++];
  for(int i=0;i<numelement;i++;rightend--)
  a[rightend]=tmparray[rightend];
 }
 };
 void main()
 {
  sort<int>s;
  inth.a[20],choice,b[20],c[10],d[20],e[20],f[20],g[20],dec;
  clrscr();
  cout<<"Enter the no: of elements:"<<endl;
  cin>>n;
  for(int i=0;i<n;i++)
  {
   cin>>a[i];
   b[i]=a[i];
   c[i]=a[i];
   d[i]=a[i];
   e[i]=a[i];
  }
  do
  {
   clrscr();
   cout<<"\t\t\t***MENU***\n";
   cout<<"1.bubble\n2.insertion\n3.selection\n4.merge\n"<<endl;
   cout<<"\t\t\tenter your choice:";
   cin>>choice;
   switch(choice)
   {
    case 1:
       cout<<"\n element before sorting:\n"<<endl;
       s.display(a,n);
       s.bubble(a,n);
       cout<<"\n elements after sorting:\n"<<endl;
       s.display(a,n);
       getch();
       break;
    case 2:
       cout<<"elements before sorting:\n"<<endl;
       s.display(b,n);
       s.insertion(b,n);
       cout<<"\nelement after sorting:\n"<<endl;
       display(b,n);
       getch();
       break;
    case 3:
       cout<<"\n element before sorting:\n"<<endl;
       s.display(c,n);
       s.selection(c,n);
       cout<<"elements after sorting:\n"<<endl;
       s.display(c,n);
       getch();
       break;
    case 4:
       cout<<"elements before sorting:\n"<<endl;
       s.display(d,n);
       s.mergesort(d,n);
       cout<<"\n Element after sorting:\n"<<endl;
       s.displsy(d,n);
       getch();
       break;
   }
  }
 while(choice<4);
 getch();
}

RESULT :
       Thus the c++ program to perform various sorting operations using templates has been executed and the output is verified successfully.