Friday, 4 March 2011

Bubble Sort using Template.


#include<iostream.h>
#include<conio.h>
template<class bubble>                              //Template function for bubble sort
void bubb_srt(bubble *arr,int l)
{
for(bubble i=0;i<=l;i++)
{
for(bubble j=0;j<=l-i-1;j++)
{
if(arr[j]>arr[j+1])
{
swap(arr[j],arr[j+1]);
}
 }
}
 }
 template <class bubble>                       //Template function for swap
 void swap(bubble &a, bubble &b)
  {
bubble temp=a;
a=b;
b=temp;
}
 void main()
 {
int n,a[100];
cout<<"Enter number of terms:"<<endl;
cin>>n;
cout<<"Enter elements into the array:"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
bubb_srt(a,n);
cout<<endl<<"The sorted array:"<<endl;
for(int j=0;j<n;j++)
{
cout<<a[j]<<" ";
}
 }