Bubble Sort

The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions. Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass.

The code for the sort is as follows:

#include<iostream>
using namespace std;
int main()
{
int a[50],n,i,j,temp,l;
cout<<"enter the size of array (max 50)";
cin>>n;
cout<<"n enter the array";
for(i=1;i<=n;i++)
cin>>a[i];
cout<<"n sorted array is";
for(j=1;j<=n-1;j++)
{
for(int k=j+1;k<=n;k++)
{
if(a[j]>a[k])
{
temp=a[j];
a[j]=a[k];
a[k]=temp;
}
}
}
for(l=1;l<=n;l++)
cout<<a[l]<<"t";
return 0;
}

Output:
Screenshot from 2013-09-29 18:59:45

Leave a Reply

Follow

Get notified with the latest posts

Plugin Supporter WordPress Post Navigation
%d bloggers like this: