Show Mobile Navigation

Saturday, December 08, 2012

C++ Program to Sort an Array by using Bubble Sort

Anonymous - 16:48

C++ Program to sort an Array by using Bubble sort

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a[50],n,i,j,temp,small,pos;
cout<<"How many elements of Array you want to create?";
cin>>n;
cout<<"Enter elements of Array ";
for(i=0;i<n;++i)
cin>>a[i];

for(i=0;i<n;++i)
{
small=a[i];
for(j=i+1;j<n;++j)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
cout<<"\nArray after sorting is ";
for(i=0;i<n;++i)
cout<<a[i]<<" ";
getch();
}

0 comments:

Post a Comment

Comments : Read Them Or Add One to promote us