program to sort the elements through Bubble Sort Method in C++



//Write a program to sort the elements through Bubble Sort
#include
#include
class bubble
{
private:
int r,data[25],i,k,ptr;
public:
void getdata();
void sort();
void showdata();
};
void bubble::getdata()
{
cout< cin>>r;
cout< for(i=0;i {
cin>>data[i];
}
}
void bubble::sort()
{
int temp;
for(k=0;k {
ptr=0;
while(ptr<=r-k)
{
if(data[ptr]>data[ptr+1])
{
temp=data[ptr];
data[ptr]=data[ptr+1];
data[ptr+1]=temp;

}
ptr++;
}

}
}
void bubble::showdata()
{
cout< for(int i=0;i {
cout< }
}
void main()
{
clrscr();
bubble b;
b.getdata();
b.sort();
b.showdata();
getch();

}





****************Output***********************

Enter the range of an array:5


Enter the elements of an array:5 4 3 2 1


Sorting order is :1 2 3 4 5

No comments:

Post a Comment