Read an array and sort them in ascending order

Write a program to read an array and sort them in ascending order

#include<stdio.h>

int main() {
	int a[15],n,i,j,temp;

	printf("How many numbers?\n");
	scanf("%d",&n);

	printf("Enter array element: \n");
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}

	for(i=0;i<n-1;i++) {
		for(j=i+1;j<n;j++) {
			if(a[i]>a[j]){ // For descinding order, sign is '<'
				temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
		}
	}

	printf("In ascending order.\n");
	for(i=0;i<n;i++)
	printf("%d ",a[i]);

	return 0;
}

Output

How many numbers?
5
Enter array element:
34
66
88
23
11
In ascending order.
11 23 34 66 88

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments