In this post, bubble sort is written in C++ using class.
#include<iostream>
using namespace std;
class BubbleSort
{
int a[20];
public:
int *bubblesort(int *, int);
};
int* BubbleSort :: bubblesort( int 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;
}
}
}
return a;
}
int main()
{
int a[10], size, i, *sa; // sa-> sorted array
BubbleSort bs;
cout<<” Enter Size of Arrayn”;
cin>> size;
cout<< “Enter Data Elementsn”;
for(i=0; i<size; i++)
{
cin>>a[i];
}
sa= bs.bubblesort(a,size);
cout<<” Sorted Arrayn”;
for(i=0; i<size; i++)
{
cout<< sa[i];
cout << “n”;
}
return 0;
}
_______________________________
For output see the video