WAP for bubble sort
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[20],i,j,n,temp;
cout<<"\n Enter size of array :";
cin>>n;
cout<<"\n Enter element of array ";
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<<"\n The sorted array are :";
for(i=1;i<=n;i++)
cout<<" "<<a[i];
getch();
clrscr();
}
/* output
Enter size of array :6
Enter element of array 12 45 32 14 10 26
The sorted array are : 10 12 14 26 32 45
No comments:
Post a Comment