C Program to Sort the Elements of an Array in Descending Order (original) (raw)
Last Updated : 20 Nov, 2024
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;
}
`
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 Content
Using Selection Sort
Selection 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;
}
`
Using Bubble Sort
In 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;
}
`
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 Find Largest Element in an Array In this article, we will learn how to find the largest element in the array using a C program.The simplest method to find the largest element in the array is by iterating the array and comparing each element with the assumed maximum and updating it when the element is greater.C#include <stdio.h 3 min read
- C Program to Find the Maximum and Minimum Element in the Array In this article, we will discuss different ways to find the maximum and minimum elements of the array in C.The simplest method to find the maximum and minimum element of the array is iterates through the array and compare each element with the assumed minimum and maximum and update them if the curre 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 Find the Range of Numbers in an Array in C? The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra 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
- How to Sort an Array of Structs with qsort in C? Sorting C arrays of structs becomes important for many kinds of applications and one common library function that may be used for this is qsort. In this article, we will learn how to use qsort() with an array of structs in C. For Example, Input: struct Person people[] = {{"Person1", 21}, {"Person2", 2 min read