Saturday 1 August 2015

Bubble Sort Program


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,temp;
cout<<"\nEnter the no. of elements you want:";
cin>>n;
int *a=new int[n];  //dynamically initialize array
cout<<"\nEnter the elements:\n";
for(int i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
 for(int j=0;j<n-i-1;j++)
 {
  if(a[j]>a[j+1])
  {
   temp=a[j];
   a[j]=a[j+1];
   a[j+1]=temp;
  }
 }
}
cout<<"\nSorted list of given integers is(in ascending order) :\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}



No comments:

Post a Comment