C++ program to sort list of numbers using Bubble sort algorithm.
C++ program to implement bubble sort
#include<iostream>
using namespace std;
class sort_list
{
int data[20];
int n;
public:
void get_data();
void bubble_sort();
void display();
};
void sort_list::get_data()
{
cout<<"How many elements two be entered ";
cin>>n;
cout<<"Enter the elements ";
for(int i=0;i<n;i++)
cin>>data[i];
}
void sort_list::bubble_sort()
{
int temp;
for(int i=1;i<n;i++)
{
for(int y=0;y<n-i;y++)
{
if(data[y]>data[y+1])
{
temp=data[y+1];
data[y+1]=data[y];
data[y]=temp;
}
}
}
}
void sort_list::display()
{
cout<<"Sorted list : ";
for(int i=0;i<n;i++)
cout<<data[i]<<" ";
}
int main()
{
sort_list sort;
sort.get_data();
sort.bubble_sort();
sort.display();
return 0;
}
using namespace std;
class sort_list
{
int data[20];
int n;
public:
void get_data();
void bubble_sort();
void display();
};
void sort_list::get_data()
{
cout<<"How many elements two be entered ";
cin>>n;
cout<<"Enter the elements ";
for(int i=0;i<n;i++)
cin>>data[i];
}
void sort_list::bubble_sort()
{
int temp;
for(int i=1;i<n;i++)
{
for(int y=0;y<n-i;y++)
{
if(data[y]>data[y+1])
{
temp=data[y+1];
data[y+1]=data[y];
data[y]=temp;
}
}
}
}
void sort_list::display()
{
cout<<"Sorted list : ";
for(int i=0;i<n;i++)
cout<<data[i]<<" ";
}
int main()
{
sort_list sort;
sort.get_data();
sort.bubble_sort();
sort.display();
return 0;
}
No comments:
Post a Comment