Template BUBBLE SORT Function in C++.
CODING:
#include<iostream.h>
#include<conio.h>
template<class bubble>
void bubble(bubble a[], int n)
{
int j,k,temp=0;
for(j=n-1;j>0;j--)
{
for(k=0;k<j;k++)
{
if(a[k]>a[k+1])
{
temp=a[k];
a[k]=a[k+1];
a[k+1]=temp;
}
}
}
}
void main()
{
int d[6],i;
char c[6];
clrscr();
cout<<"\nENTER THE 6 DIGIT:\n";
for(i=0;i<6;i++)
{
cout<<'['<<i<<']';
cin>>d[i];
}
bubble(d,6);
cout<<"SORTED DIGIT:";
for(i=0;i<6;i++)
{
cout<<endl<<'['<<i<<']'<<d[i];
}
cout<<"\nENTER THE 6 CHARACTER:\n";
for(i=0;i<6;i++)
{
cout<<'['<<i<<']';
cin>>c[i];
}
bubble(c,6);
cout<<"SORTED CHARACTER:";
for(i=0;i<6;i++)
{
cout<<endl<<'['<<i<<']'<<c[i];
}
getch();
}
CODING:
#include<iostream.h>
#include<conio.h>
template<class bubble>
void bubble(bubble a[], int n)
{
int j,k,temp=0;
for(j=n-1;j>0;j--)
{
for(k=0;k<j;k++)
{
if(a[k]>a[k+1])
{
temp=a[k];
a[k]=a[k+1];
a[k+1]=temp;
}
}
}
}
void main()
{
int d[6],i;
char c[6];
clrscr();
cout<<"\nENTER THE 6 DIGIT:\n";
for(i=0;i<6;i++)
{
cout<<'['<<i<<']';
cin>>d[i];
}
bubble(d,6);
cout<<"SORTED DIGIT:";
for(i=0;i<6;i++)
{
cout<<endl<<'['<<i<<']'<<d[i];
}
cout<<"\nENTER THE 6 CHARACTER:\n";
for(i=0;i<6;i++)
{
cout<<'['<<i<<']';
cin>>c[i];
}
bubble(c,6);
cout<<"SORTED CHARACTER:";
for(i=0;i<6;i++)
{
cout<<endl<<'['<<i<<']'<<c[i];
}
getch();
}
Comments
Post a Comment