BUBBLE SORT USING TEMPLATE FUNCTIONS
<iostream>
using namespace std;
template<class T>
bubble(T a[], int n)
{
(int i=0;i < n-1;i++)
{
(int j=n-1;i < j;j--)
{
(a[j] < a[j-1])
{
swap(a[j],a[j-1]);
}
}
}
}
template<class X>
swap(X &a,X &b)
{
X temp=a;
a=b;
b=temp;
}
main()
{
x[5]={10,50,30,40,20};
y[5]={1.1,5.5,3.3,4.4,2.2};
bubble(x,5);
bubble(y,5);
std::cout <<"sorted x-array";
(int i=0;i < 5;i++)
std::cout << x[i] << " ";
std::cout << "sorted y-array";
(int j=0;j < 5;j++)
std::cout << y[j] << " ";
std::cout << "\n";
}
|