C++ PRACTICAL PROGRAMS
Class 12 Computer Science | ASSEB
Program to sort the elements of an array using Bubble sort
#include<iostream>
using namespace std;
int main( )
{
int a[50], n, temp;
cout<<”Enter number of elements”;
cin>>n;
cout<<”\n Enter elements”;
for (int i=0; i<n; i++)
cin>>a[i];
for (int i=0; i<n-1; 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<<”Sorted array”;
for (int i=0; i<n; i++)
cout<<a[i]<<” “;
return 0;
}
Output
Enter number of elements7
Enter elements2 4 1 9 5 6 8
Sorted array1 2 4 5 6 8 9