C program to sort the array elements in descending order – In this article, we will brief in on the multitude of ways to sort the array elements in a descending order in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself. If you have any doubts related to C programs to sort the array in descending order leave a comment here.
The ways described in this particular piece are as follows:
- Using Standard Method
- Using Function
As we all know, an array is an arrangement or a sequence of a bunch of elements in any given order whatsoever. The information stored is used in various means in C programming.
As you can see in the image uploaded, the size of the array is entered first up.
The size of the array given in this case is 6.
Afterwards, the elements in the array are listed.
The elements in this particular array are 2, 1, 5, 6, 0, 10.
Hence, the descending order of the elements entered is 10, 6, 5, 2, 1, 0.
Thus, the various ways to set a bunch of elements of an array in descending order are as follows:
Using Standard Method
- Read the size of the array and store the value into the variable n.
2) scanf reads the entered elements one by one and stores the elements in the array using for loop for(i=0;i<n;i++) as scanf(“%d”&a[i]).
3) Compare the two elements a[j] and a[j+1] and find the least element then swap the elements if a[j] is the least element compare with a[j+1]. The code is as follows
for loop iterates from i=0 to i<n-1
a) for loop iterates from j=0 to j<n-i-1
find the least from a[j] and a[j+1] and swap the two elements, if a[j] is least element than a[j+1].Repeat until all iterations of j.
b) After all iterations of i, the sorted array will be generated in which the elements are in descending order.
4) Print the descending order array elements.
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 29 30 31 32 33 34 35 36 37 38 39 40 |
#include <stdio.h> #include <conio.h> int main() { int a[10000],i,n,j,temp; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } for(i=0; i<n-1; i++) { for(j=0; j<n-i-1; j++) { if(a[j]<a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("\narray elements in descending order:\n "); for(i=0; i<n; i++) { printf("%d ",a[i]); } } |
Output:
1 2 3 4 5 |
Enter size of the array : 6 Enter elements in array : 2 1 5 6 0 10 array elements in descending order: 10 6 5 2 1 0 |
Using Function
- The main() function calls the sort() function by passing array a[],size of the array as arguments.
2) The sort method arranges the array elements in descending order as follows,
a) for loop iterates from i=0 to i<n-1
b) Inner for loop iterates from j=0 to j<n-i-1
Compare adjacent elements a[j] and a[j+1],if a[j] is less then a[j+1] then swap the two elements.Repeat this step until j<n-i-1.
Repeat the above step until all iterations of i.
3) After all iterations of i, we will get descending order array.
4) The main() calls the print(a,n) to print the descending order array elements.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <stdio.h> #include <conio.h> int sort(int *a,int n) { int i,j,temp; for(i=0; i<n-1; i++) { for(j=0; j<n-i-1; j++) { if(a[j]<a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } print(int *a,int n) { int i; for(i=0; i<n; i++) { printf("%d ",a[i]); } } int main() { int a[10000],i,n,key; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } sort(a,n); print(a,n); } |
1 2 3 |
Enter size of the array : 6 Enter elements in array : 1 0 8 5 -1 6 array elements in descending order:8 6 5 1 0 -1 |