Google Search

Tuesday, October 26, 2010

INSERTION SORT USING TEMPLATES


PROGRAM:

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
template <class t>
class insertion
{
t a[25];
public:
void get(int);
void sort(int);
void display(int);
};
template <class t>
void insertion<t>::get(int n)
{
int i;
cout<<"\nEnter the array elements:";
for(i=0; i<n;i++)
cin>>a[i];
}
template <class t>
void insertion <t>::display(int n)
{
int i;
cout<<"\nThe sorted array is: \t";
for(i=0;i<n;i++)
cout<<a[i]<<setw(10);
}
template <class t>
void insertion <t>::sort(int n)
{
int i,j;
t temp;
for(i=1;i<n;i++)
{
j=i;
while(j>=1)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
j--;
}
}
}
void main()
{
int n,m;
insertion<int> i1;
insertion<float> i2;
clrscr();
cout<<"\nInsertion Sort on Integer Values\n";
cout<<"\nEnter the size of array:\t";
cin>>n;
i1.get(n);
i1.sort(n);
i1.display(n);
cout<<"\n\nInsertion Sort on Float Values\n";
cout<<"\nEnter the size of array:\t";
cin>>m;
i2.get(m);
i2.sort(m);
i2.display(m);
getch();
}




0 comments:

Post a Comment