How to Sort an Array in Descending Order using STL in C++?
Last Updated :
23 Oct, 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 how to sort an array in descending order using STL in C++.
Examples
Input: arr[] = {11, 9, 45, 21};
Output: 78 45 23 21 11 9
Explanation: As the elements are sorted in decreasing order.
Input: arr[] = {11, 9, 56, 1, 89, 7};
Output: 89 56 11 9 7 1
Explanation: As the elements are sorted in decreasing order.
C++ STL provides the following different methods to sort an array in descending order:
Using std::sort() Function
C++ STL provides the std::sort() algorithm to sort the given range of elements. We can also sort the array using this function. By default, it sorts the array in ascending order, but we can change it to descending order using custom comparator function.
Syntax
std::sort(first, last, comp);
where, first and last are the iterator to the beginning and the end of the range, and comp is the custom comparator.
Example
C++
// C++ Program to sort an array in descending
// order using std::sort()
#include <bits/stdc++.h>
using namespace std;
// Comparator function
bool comp (int a, int b) {
return a > b;
}
int main() {
int arr[] = {11, 9, 45, 21};
int n = sizeof(arr) / sizeof(arr[0]);
// sort an array in descending order along
// with custom comparator
sort(arr, arr + n, comp);
for (auto i : arr)
cout << i << " ";
return 0;
}
Time Complexity: O(n * log n), where n is the size of the array.
Auxiliary Space: O(1)
Note: We also can use the std::greater<>() functor as comparator in this code.
Using std::stable_sort() Function
We can also use std::stable_sort() function is similar to the std::sort() function but preserve the relative order of equal elements in the array while sorting. It also sorts the array in the ascending order by default, so we have to provide custom comparator
Syntax
std::stable_sort(first, last, comp);
where, first and last are the iterator to the beginning and the end of the range, and comp is the custom comparator.
Example
C++
// C++ Program to sort an array in descending
// order using std::stable_sort()
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {11, 9, 45, 21};
int n = sizeof(arr) / sizeof(arr[0]);
// sort an array in descending order
stable_sort(arr, arr + n, greater<>());
for (auto i : arr)
cout << i << " ";
return 0;
}
Time Complexity: O(n * log n), where n is the size of the array.
Auxiliary Space: O(1)
Using std::partial_sort() Function
We can sort an array in descending order using std::partial_sort() function provided by STL in C++. This function is generally used to sort a part of the given range, but we can modify it to sort the whole range. We also have to provide custom comparator for sorting in decreasing order.
Syntax
std::partial_sort(first, middle, last, comp);
where, first and last are the iterator to the beginning and the end of the range, and comp is the custom comparator. middle is the iterator to the point till which we have to sort starting from first.
Example
C++
// C++ Program to sort an array in descending
// order using std::partial_sort()
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {11, 9, 45, 21};
int n = sizeof(arr) / sizeof(arr[0]);
// Sort array in descending order
partial_sort(arr, arr + n, arr + n,
greater<int>());
for (auto i: arr)
cout << i << " ";
return 0;
}
Time Complexity: O(n * log n), where n is the size of the array.
Auxiliary Space: O(1)
Similar Reads
How to Reverse an Array using STL in C++? Reversing an array means rearranging its elements so that the first element becomes the last, the second element becomes the second last, and so on. In this article, we will learn how to reverse an array using STL in C++.The most efficient way to reverse an array using STL is by using reverse() func
2 min read
How to Sort an Array of Strings Using Pointers in C++? In C++, sorting an array of strings using pointers is quite different from normal sorting because here the manipulation of pointers is done directly, and then according to which string is pointed by the pointer the sorting is done. The task is to sort a given array of strings using pointers. Example
2 min read
How to Sort Vector in Ascending Order in C++? Sort a vector in ascending order means arranging the elements of vector in such a way that the first element will be smallest, second element will be second smallest and so on. In this article, we will learn different ways to sort the vector in ascending order in C++.The most efficient way to sort t
3 min read
How to Find the Second Smallest Element in an Array in C++? In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element
3 min read
C++ Program to Sort the Elements of an Array in Ascending Order Here, we will see how to sort the elements of an array in ascending order using a C++ program. Below are the examples: Input: 3 4 5 8 1 10Output: 1 3 4 5 8 10 Input: 11 34 6 20 40 3Output: 3 6 11 20 34 40 There are 2 ways to sort an array in ascending order in C++: Brute-force Approach Using Bubble
4 min read
How to Find the Smallest Number in an Array in C++? In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma
2 min read