Program for Bubble Sort in C++ array

Bubble sort is a sorting technique in which each pair of adjacent elements are compared, if they are in wrong order we swap them.sort an array in ascending order using bubble sort in C++ programming, you have to ask the user to enter the array size then ask to enter array elements.

Program for Bubble Sort in C++ array

#include<iostream>
using namespace std;
int main()
{
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the array elements: ";
for(i=0;i<n;++i)
cin>>a[i];
for(i=1;i<n;++i)
{
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
cout<<"Array after bubble sort:";
for(i=0;i<n;++i)
cout<<" "<<a[i];
return 0;
}

Output of Program

Enter the size of array: 5
Enter the array elements: 1
2
7
0
5
Array after bubble sort: 0 1 2 5 7