Bubble sort

Solution:

#include <iostream>
using namespace std;
int main()
{
int i,j;
int n,temp; // In case of dealing with 'char', instead of 'int', 'char' would have to be written
cin>>n;
int a[n];
for(i=0;i<n;i++) cin>>a[i];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++) cout<<a[i]<<endl;
return 0;
}
view raw Bubble sort.cpp hosted with ❤ by GitHub

Leave a comment