Search This Blog

Bubble Sort in C++

// Bubble Sorting
#include<iostream.h>
void main()
{
   int a[20];
   int i,j,n,t;
   cout<<"\n Enter the number of elements(>20) : ";
   cin>>n;
   if(n>20)
      cout<<"\n List size cannot exceed 20 ";
   else
   {
     cout<<"\n Enter the elements : \n ";
     // accepting the list of elements
     for(i=0;i<n;i++)
       cin>>a[i];
     // Sorting
     for(i=0;i<n;i++)
     {  cout<<"\n Pass : "<<i+1;
    for(j=0;j<(n-i);j++)
{
 if(a[j+1]<a[j])
 {
    t=a[j+1];
    a[j+1]=a[j];
    a[j]=t;
 }
}
cout<<"\n";
for(int k=0;k<n;k++)
  cout<<"\t"<<a[k];
     }
     cout<<"\n The sorted list is : ";
     for(i=0;i<n;i++)
       cout<<"\t"<<a[i];
     cout<<"\n Have a nice day ";
 }
 }