Wednesday, July 6, 2011

Program to sort the array in Ascending order using Bubble sort using a function.


#include<iostream.h>
#include<conio.h>
void bubble(int ar[100],int r)
{
  int i,j,t=0;
  for(i=0;i<r;i++)
  {
for(j=i;j<r;j++)
{
    if(ar[i]>ar[j])
    {
      t=ar[i];
      ar[i]=ar[j];
      ar[j]=t;
    }
}
  }
  cout<<"The Sorted array:"<<endl;
  for(i=0;i<r;i++)
  {
       cout<<ar[i]<<endl;
  }
}
void main()
{
  clrscr();
  int ar[100],r=0,i;
  for(i=0;i<100;i++)
  {
    ar[i]=0;
  }
  cout<<"Enter the range:(less than 100) "<<endl;
  cin>>r;
  cout<<"Enter the elements of the array:"<<endl;
  for(i=0;i<r;i++)
  {
       cout<<"Enter element number:"<<(i+1)<<endl;
       cin>>ar[i];
  }
  bubble(ar,r);
  getch();
}

No comments:

Post a Comment