Write a C++ program to sort an array elements in ascending order.
Program Code:
#include<iostream>
using namespace std;
int main()
{
int i, j, temp;
int a[10];
cout<<"Enter elements in the array : \n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"\n Data elements before sorting in the array : ";
for(j=0;j<10;j++)
{
cout<<a[j]<<" ";
}
for(i=0;i<10;i++)
{
for(j=0;j<10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\n Data elements after sorting in the array : ";
for(j=0;j<10;j++)
{
cout<<a[j]<<" ";
}
return 0;
}
The program is tested on www.jdoodle.com
Output:
Enter elements in the array : 3 4 5 1 2 6 7 8 9 1
Data elements before sorting in the array : 3 4 5 1 2 6 7 8 9 1
Data elements after sorting in the array : 1 1 2 3 4 5 6 7 8 9
Thanks
Mukesh Rajput
Post A Comment:
0 comments: