Sort an Array in Ascending Order
Here you will get and learn the program code of C++ program to sort an array in ascending order. This is an easy sorting example to sort the elements in single dimensional array.
C++ Program to Sort an Array in Ascending Order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include<iostream> using namespace std; int main() { int i, j,temp,a[10]; cout<<"\nEnter 10 numbers for sorting :"; for(i=0;i<10;i++) { cin>>a[i]; } for(i=0;i<10;i++) { for(j=i+1;j<10;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } cout<<"\nSorted Elements List ...\n"; for(i = 0; i<10; i++) { cout<<a[i]<<" "; } } |
Output
Enter 10 numbers for sorting :3
5
2
7
6
1
9
8
15
12
Sorted Elements List …
1 2 3 5 6 7 8 9 12 15
Check out our other C++ programming Examples