Thursday, November 25, 2010

TEMPLATES-BUBBLE SORT


/* Bubble Sort Using Templets */
#include<iostream>
using namespace std;
template<class T>
void bubble(T a[],int n)
{
for(int i=0;i<n-1;i++)
for(int j=n-1;i<j;j--)
if(a[j]<a[j-1])
{
swap(a[j],a[j-1]);
}
}
template <class x>
void swap(x *a,x*b)
{
x temp;
temp=&a;
&a=&b;
&b=temp;
}
int main()
{
int x[5],i,j;
float y[5];
cout<<"enter integer array:\n";
for(i=0;i<5;i++)
cin>>x[i];
cout<<"enter float array:\n";
for(j=0;j<5;j++)
cin>>y[j];
bubble(x,5);
bubble(y,5);
cout<<"sorted x-array \n";
for(i=0;i<5;i++)
cout<<x[i]<<" "<<"\n";
cout<<"sorted y-array \n";
for(j=0;j<5;j++)
cout<<y[j]<<" "<<"\n";
return(0);
}

No comments:

Post a Comment

FOLLOWERS