/* The following C++ Program Code is for the implementation of bubble sorting algorithm. It also use the concept of the template class, so no need of worry about the input array's data type. The user is first asked about the total number of elements that are to be in the list. Then he enters the data, after it the program again process the data, & shows the output sorted array. */
#include<iostream>
#include<stdlib.h>
using namespace std;
template <class T>
void bubble(T a[],int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
int main()
{
int a[100],i,n;
cout<<"\nEnter number of element : ";
cin>>n;
cout<<"\n Enter elements( Use Spacebar as separator )\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n\nBefore sorting \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
bubble(a,n);
cout<<"\n\nAfter sorting \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
cout<<endl;
system("pause");
return 0;
}
Screenshot:
#include<iostream>
#include<stdlib.h>
using namespace std;
template <class T>
void bubble(T a[],int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
int main()
{
int a[100],i,n;
cout<<"\nEnter number of element : ";
cin>>n;
cout<<"\n Enter elements( Use Spacebar as separator )\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n\nBefore sorting \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
bubble(a,n);
cout<<"\n\nAfter sorting \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
cout<<endl;
system("pause");
return 0;
}
Screenshot:
0 comments:
Post a Comment