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