Today, we are going to share information about Sorting Elements in an Array in C language. The sorting is of two types, one is arranging elements in ascending order and the other is arranging elements in descending order. We will sort elements in both ways by ascending and by descending order in arrays in C language.
Sorting Elements in an Array
There are two methods of Sorting Elements in an Array, one is ascending order and the other is descending. We will apply both methods to the array in the C language. First, we will do sorting in ascending order, then after, we will sort in descending order.
Ascending Order
In this method, elements of arrays will be arranged from lower to higher elements or smaller to greater elements. This method is the opposite of descending order.
Program
Write a C program that will sort elements of an array in ascending order.
#include<stdio.h> int main() { int size,a[10],i,j,temp; printf("enter size of array: "); scanf("%d",&size); printf("enter elements of array: "); for(i=0;i<size;i++) { scanf("%d",&a[i]); } for(i=0;i<size;i++) { for(j=i+1;j<size;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } printf("%d ",a[i]); } }
Output
First of all, we have to take integer type variables which will be i, j, temp, size, and a[10]. We have taken I and j for applying loop, temp is used for swapping values, size is used for giving size to an array, and a[10] is used for taking elements of an array.
First of all, we will take the size of an array with the help of a size variable and the size will be stored in a size variable, then we will take elements of the array from the user with the help of a[10] variable. elements will be taken with the help of for loop.
After that, we will apply nested for loop I and j. in the body of the inner loop, we will apply the if statement, in which we apply condition which is a[i]>a[j], and in the body of if statement, we will apply the condition of swapping values.
After that, in the body of the outer loop, we will print out these elements which will be in ascending order, we will output a[i] because it is the variable that is used to store values of variables and other variables are used for running loops.
Descending Order
In this method, elements of arrays will be arranged from higher to lower elements or from greater to smaller elements. This method is the opposite of ascending order.
Program
Write a C program that will sort elements of an array in descending order.
#include<stdio.h> int main() { int size,a[10],i,j,temp; printf("enter size of array: "); scanf("%d",&size); printf("enter elements of array: "); for(i=0;i<size;i++) { scanf("%d",&a[i]); } for(i=0;i<size;i++) { for(j=i+1;j<size;j++) { if(a[i]<a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } printf("%d ",a[i]); } }
Output
First of all, we have to take integer type variables which will be i, j, temp, size, and a[10]. We have taken I and j for applying loop, temp is used for swapping values, size is used for giving size to an array, and a[10] is used for taking elements of the array.
First of all, we will take the size of an array with the help of a size variable and the size will be stored in the size variable, then we will take elements of an array from the user with the help of a[10] variable. elements will be taken with the help of for loop.
In the previous article, we have explained the Restaurant Billing System in C Without Using Any Functions, After that, we will apply nested for loop I and j. in the body of the inner loop, we will apply the if statement, in which we apply condition which is a[i]<a[j], and in the body of the if statement, we will apply the condition of swapping values.
After that, in the body of the outer loop, we will print out these elements which will be in descending order, we will output a[i] because it is the variable that is used to store values of variables and other variables are used for running loops.
Difference between ascending and descending order
The basic difference between ascending and descending order is only one logic, and both programs are approximately the same but the difference is only of a relational operator. For ascending order, it will be (a[i]>a[j]) and for descending order it will be (a[i]<a[j]).
Conclusion
In the above article, we have explained all the procedures of Sorting Elements in an Array which have two types, one is ascending and other is descending method. We have both types of sorting and also we explained the basic difference between these two types.