Sort array values in ascending order in c program
Write a program to sort the given array values in a ascending order in c program.
Example : pgm.c
#include<stdio.h> int main(){ int i,j,n,a[100],temp; printf("\nEnter the Limit : "); scanf("%d",&n); for(i=0;i<n;i++){ printf("\nEnter %d Number : ",i+1); scanf("%d",&a[i]); } for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ if(a[i]>a[j]){ temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("\nAfter Ascending:"); for(i=0;i<n;i++){ printf("\n%d",a[i]); } return 0; }
Output:
Enter the Limit : 5
Enter 1 Number : 5
Enter 2 Number : 2
Enter 3 Number : 4
Enter 4 Number : 1
Enter 5 Number : 3
After Ascending:
1
2
3
4
5
Enter 1 Number : 5
Enter 2 Number : 2
Enter 3 Number : 4
Enter 4 Number : 1
Enter 5 Number : 3
After Ascending:
1
2
3
4
5