C Program to Sort the Elements of an Array in Descending Order Last Updated : 20 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Sort an array in descending order means arranging the elements in such a way that the largest element at first place, second largest at second place and so on. In this article, we will learn different ways to sort an array in descending order in C.The simplest method to sort the array in descending order is by using the in-built library function qsort(). Let's take a look at an example: C #include <stdio.h> #include <stdlib.h> // Comparator function to sort in descending order int comp(const void *a, const void *b) { return (*(int*)b - *(int*)a); } int main() { int arr[] = {1, 3, 5, 4, 2}; int n = sizeof(arr) / sizeof(arr[0]); // Sorting the array using qsort qsort(arr, n, sizeof(int), comp); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } Output5 4 3 2 1 qsort() function uses quicksort algorithm and needs a comparator function for guiding it to sort the array in descending order.C language only provides the built-in implementation of quick sort algorithm. If you want to use other sorting algorithm apart from quicksort, you have to manually implement it as shown:Table of ContentUsing Selection SortUsing Bubble SortUsing Selection SortSelection sort is a simple sorting algorithm that repeatedly finds the maximum element from the unsorted part of the array and places it at its position in the sorted part of the array until the complete array is sorted. C #include <stdio.h> // Selection sort implementation void sort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { int max = i; for (int j = i + 1; j < n; j++) { if (arr[j] > arr[max]) max = j; } if (max != i) { int temp = arr[max]; arr[max] = arr[i]; arr[i] = temp; } } } int main() { int arr[] = {1, 3, 5, 4, 2}; int n = sizeof(arr) / sizeof(arr[0]); // Calling selection sort on arr sort(arr, n); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } Output5 4 3 2 1 Using Bubble SortIn Bubble Sort Algorithm, we repeatedly swap adjacent elements if they are in the wrong order. This process is repeated until the array is sorted in descending order. C #include <stdio.h> // Bubble sort implementation void sort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] < arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { int arr[] = {1, 3, 5, 4, 2}; int n = sizeof(arr) / sizeof(arr[0]); // Perform bubble sort sort(arr, n); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } Output5 4 3 2 1 Comment More infoAdvertise with us Next Article C program to sort an array using pointers L laxmigangarajula03 Follow Improve Article Tags : C Programs C Language c-array C Array Programs Similar Reads C Program to Sort an Array in Ascending Order Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element.The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example:C 3 min read C program to sort an array using pointers Given an array of size n, the task is to sort this array using pointers in C. Examples: Input: n = 5, arr[] = {0, 23, 14, 12, 9} Output: {0, 9, 12, 14, 23} Input: n = 3, arr[] = {7, 0, 2} Output: {0, 2, 7} Approach: The array can be fetched with the help of pointers with the pointer variable pointin 2 min read How to Find the Mode of Numbers in a Sorted Array in C? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will learn how to find the mode of all elements in a sorted array of integers in C. Example: Input:myArray = {1, 2, 3, 3, 5, 5, 5, 5, 6, 7} 2 min read How to Find the Size of an Array in C? The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.The simplest method to find the size of an array in C is by using sizeof operator. First determine t 2 min read How to Sort an Array of Structs Based on a Member in C? In C, we may sometimes need to sort the array of structs based on the values of certain members. In this article, we will learn how to sort a given array of structures based on a specific member in C. For Example, Input: myArrayofStructs[] = {{"Person1", 21, 160.5}, {"Person2", 20, 175.0}, {"Person3 2 min read C Program to Sort a String of Characters Sorting a string of characters refers to the process of rearranging all the characters in the given order. In this article, we will learn how to sort a string of characters using the C program.The most straightforward method to sort a string of characters is by using the qsort() function. Let's take 3 min read Like