Sort Array in Ascending Order
Here you will get program code to sort array in ascending order in c. This example will be implemented by using 1d array in c programming.
Algorithm
Step 1 create an array list.
Step 2 Start a loop from first element to the last element of array.
Step 3 Compare every element of the array with the next element of array.
Step 4 If the current element is greater than the next element, swap their positions in the array.
Step 5 Repeat steps 3 and 4 until the entire array is sorted in ascending order.
Step 6 Print the sorted array.
Sort Array in Ascending Order in C program
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 | //C Program to Sort an Array in Ascending Order #include<stdio.h> void main () { int i, j,temp,a[10]; printf("\nEnter 10 numbers for sorting :"); for(i=0;i<10;i++) { scanf("%d",&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; } } } printf("\nSorted Elements List ...\n"); for(i = 0; i<10; i++) { printf("%d\n",a[i]); } } |
Output
Enter 10 number to sorting : 2 9 3 8 1 4 6 5 7 10
Sorted Elements List …
1
2
3
4
5
6
7
8
9
10
Check out our other C programming Examples