Wednesday, October 26, 2016

Bubble Sort Using Function Template

Tags

Code:

#include<iostream>
using namespace std;
float a[100];
template<class T>
void sort(T a[],int n)
{
int c=n-1;
T k;
for(int j=0;j<=c;j++)
for(int i=0;i<n-j-1;i++)
{
if(a[i]>a[i+1])
{
k=a[i];
a[i]=a[i+1];
a[i+1]=k;
}
}
}
main()
{
int n,i;
cout<<"ENTER THE NO OF ELEMENTS";
cin>>n;
cout<<"ENTER THE ELEMENTS";
for(i=0;i<n;i++)
cin>>a[i];
sort(a,n);
printf("AFTER SORTING\n");
for(i=0;i<n;i++)
{
cout<<a[i];
cout<<"\n";}
}
Output: