C Program to sort an array in descending order


In this program, You will learn how to sort an array in descending order in c.


An array list is: 2 3 1 7 6

After sort: 7 6 3 2 1

Example: How to sort an array in descending order in c.

#include<stdio.h>

int main() {

   int a[10];
   int n, temp;

   printf("Enter size of an array:");
   scanf("%d", &n);

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

   for (int i = 0; i < n; i++) {
       for (int j = 1 + i; j < n; j++) {
           if (a[i] < a[j]) {
               temp = a[i];
               a[i] = a[j];
               a[j] = temp;
           }
       }
   }
   printf("Array in descending:");
   for (int i = 0; i < n; i++) {
       printf("%d ", a[i]);
   }

   return 0;
}

Output:

Enter size of an array:5                                                                                                               
Enter  array elements:2 3 4 5 6                                                                                                        
Array in descending:6 5 4 3 2