Bubble Sort in Data Structures using C++
by TechVidvan Team
Program 1
// Program for Bubble sort
#include<iostream>
#define clrscr() system("cls")
using namespace std;
class Sort
{
public:
void bubblesort(int ar[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
cout<<"\n Sorted elements:\n";
for(i=0;i<n;i++)
cout<<ar[i]<<"\n";
}
};
int main()
{
int a[500],n,i,j,temp;
clrscr();
xyz:cout<<"\nEnter the limit of array";
cin>>n;
if(n<0 || n>500)
{
cout<<"\nInvalid limit pls enter again...\n";
goto xyz;
}
else
{
cout<<"\nEnter the element in array\n";
for(i=0;i<n;i++)
cin>>a[i];
Sort S;
S.bubblesort(a,n);
}
return 0;
}
// // Bubble sort
// for(i=0;i<n-1;i++)
// {
// for(j=0;j<n-i-1;j++)
// {
// if(a[j]>a[j+1])
// {
// temp=a[j];
// a[j]=a[j+1];
// a[j+1]=temp;
// }
// }
// }
// cout<<"\n Sorted elements:\n";
// for(i=0;i<n;i++)
// cout<<a[i]<<"\n";
// }
// return 0;
// }
Tags: Bubble Sortbubble sort in dsa using c++Data Structuresdsa using c++dsa using c++ practicaldsa using c++ programdsa with c++
TechVidvan Team
TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.