IMPLEMENTATION OF BUBBLE SORT - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Tuesday, October 26, 2010

IMPLEMENTATION OF BUBBLE SORT


PROGRAM:

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
template <class t>
class bubble
{
t a[25];
public:
void get(int);
void sort(int);
void display(int);
};
template <class t>
void bubble <t>::get(int n)
{
int i;
cout<<"\nEnter the array elements:";
for(i=0; i<n;i++)
cin>>a[i];
}
template <class t>
void bubble <t>::display(int n)
{
int i;
cout<<"\nThe sorted array is:\t";
for(i=0;i<n;i++)
cout<<a[i]<<setw(10);
}
template <class t>
void bubble <t>::sort(int n)
{
int i,j;
t temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void main()
{
int n,m;
bubble<int> b1;
bubble<float> b2;
clrscr();
cout<<"\n----Bubble Sort on Integer Values----";
cout<<"\nEnter the size of array:\n";
cin>>n;
b1.get(n);
b1.sort(n);
b1.display(n);
cout<<"\n\n----Bubble Sort on Float values----\n";
cout<<"\nEnter the size of array:\n";
cin>>m;
b2.get(m);
b2.sort(m);
b2.display(m);
getch();
}



No comments:

Post a Comment