Take an array as input and sort it in ascending order by Bubble Sort :-
#include<iostream.h>
#include<conio.h>
void main()
{
int a['z'],size;
cout<<"\nEnter Size of Array : ";
cin>>size;
for(int i=0;i<size;i++)
{
cout<<"\nEnter Element "<<(i+1)<<" : ";
cin>>a[i];
}
for(int i=0;i<=size;i++)
{
for(int j=0;j<size-1;j++)
{
if(a[j]>a[j+1])
{
int tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
cout<<"Array after Sorting is \n";
for(int i=0;i<size;i++)
{
cout<<"\nElement "<<(i+1)<<" : "<<a[i];
}
getch();
}
No comments:
Post a Comment