Showing posts with label Bubble Sort. Show all posts
Showing posts with label Bubble Sort. Show all posts

Wednesday, July 6, 2011

Program to sort the given array in Ascending order using Bubble sort.


#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int ar[100];
  int r,t=0,i,j;
  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];
  }
  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;
  }
  getch();
}