### Sorting Using Template - C++

Program:

#include<iostream>
using namespace std;
template<class t>
void sort(t a[],int n)
{
int i,j;t tem;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]<a[j+1])
{
tem=a[j];
a[j]=a[j+1];
a[j+1]=tem;
}
cout<<"\n\t--------After Sorting---------\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];
}

int main()
{
int i,n,a[25],ch;float b[25];bool exit=false;
cout<<"\n\t------SORTING USING TEMPLATE-----------\n\nEnter the number of elements: ";
cin>>n;

do{
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\t------INT TYPE-----------\n\n";
cout<<"\nEnter elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
sort(a,n);
break;

case 2:
cout<<"\n\t------FLOAT TYPE-----------\n\n";
cout<<"\nEnter elements:\n";
for(i=0;i<n;i++)
cin>>b[i];
sort(b,n);
break;
case 3:

default:
cout<<"Exiting..... \n\t\t\t\t\t\t...www.2k8618.blogspot.com\n";
exit=true;
}
}while(!exit);
return 0;
}

Output:

nn@ubuntu:~\$ ./a.out

------SORTING USING TEMPLATE-----------

Enter the number of elements: 5

1.Integer Sorting
2.Floating point Sorting
3.Exit

------INT TYPE-----------

Enter elements:
5
4
3
2
1

--------After Sorting---------

5 4 3 2 1

1.Integer Sorting
2.Floating point Sorting
3.Exit

------FLOAT TYPE-----------

Enter elements:
1.9
1.2
1.0
1.4
1.7

--------After Sorting---------

1.9 1.7 1.4 1.2 1

1.Integer Sorting
2.Floating point Sorting
3.Exit