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

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: