A program to initialize a 1-D array and sort it in ascending order using Bubble Sort method.

 

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

void main()
{
clrscr();
int a[10],i,j,temp;

cout<<"Enter elements of the array:"<<endl;
for(i=0;i<10;i++)
{
cout<<"Enter element no."<<i+1<<": ";
cin>>a[i];
}

for(i=0;i<9;i++)
{
for(j=0;j<9-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

cout<<"\nElements of the sorted array:"<<endl;
for(i=0;i<10;i++)
cout<<a[i]<<" ";

gotoxy(25,24); cout<<"Press any key to continue......";
getch();
}

 

< Back