Wednesday 28 November 2012

WAP to implement Bubble Sort


//To sort the elements of linear array using bubble sort

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
int a[100],n,i,j,item,temp;
cout<<"Enter the no.of elements in array ";
cin>>n;
cout<<"\nEnter the elements \n";
for(i=1;i<=n;i++)
{
cin>>a[i];
 }
for(i=1;i<=n-1;i++)
{
  for(j=1;j<=n-i;j++)
{
  if (a[j]>a[j+1])
   {
           temp=a[j];
           a[j]=a[j+1];
           a[j+1]=temp;
      }   }   }
cout<<"\nThe sorted array elements are: \n";
for(i=1;i<=n;i++)
{
  cout<<a[i]<<" ";
  }
getch();
 }

Output:


No comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...