Sort Vector in Descending Order Using STL in C++



The problem is to sort a vector in descending order using C++'s Standard Template Library(STL). Sorting in descending order means rearranging the elements of the vector so that the largest elements come first, followed by smaller elements, all the way down to the smallest element at the end of the vector.

Let's say we have the following vector of integers:

vector<int> v = {4, 2, 9, 1, 7};

We want to sort this vector so that the elements are arranged in descending order:

v = {9, 7, 4, 2, 1};

In this article, we will show you how to sort a vector in descending order using C++ Standard Template Library (STL).

Approaches for Sorting a Vector in Descending Order

There are different ways to sort a vector in descending order using STL. We will look at three main methods:

Using sort() with a custom comparator

The C++ Standard Library provides a sort() function that sorts vectors in ascending order by default. To sort in descending order, we can use a custom comparator, which is a function or lambda that tells the sort() function how to compare the elements.

Example

In this example, we first define a vector of integers, then print the original vector. After that, we use sort() with a lambda function to sort the elements in descending order. Finally, we display the sorted vector.

#include <iostream>
#include <vector>
#include <algorithm>  // For sort()

using namespace std;

int main() {
    // Initialize a vector with some integers
    vector<int> vec = {5, 2, 8, 1, 9, 3};

    // Display the original vector
    cout << "Original vector: ";
    for (int num : vec) {
        cout << num << " ";
    }
    cout << endl;

    // Sort the vector in descending order using a custom comparator (lambda)
    sort(vec.begin(), vec.end(), [](int a, int b) {
        return a > b;  // Return true if a is greater than b (descending order)
    });

    // Display the sorted vector
    cout << "Sorted vector in descending order: ";
    for (int num : vec) {
        cout << num << " ";
    }
    return 0;
}

Output

The output of the code displays the sorted vector in descending order as expected.

Original vector: 5 2 8 1 9 3 
Sorted vector in descending order: 9 8 5 3 2 1

Time Complexity: O(n log n) due to the sorting algorithm (quicksort or similar).

Space Complexity: O(log n) due to the stack space used by the recursive sorting algorithm.

Using reverse() after sorting in ascending order

Another way to achieve descending order is by first sorting the vector in ascending order and then reversing it.

Example

In this example, we first use the sort() function to sort the vector in ascending order. After that, we use the reverse() function to reverse the order, achieving the desired descending order.

#include <iostream>
#include <vector>
#include <algorithm>  // For sort() and reverse()

using namespace std;

int main() {
    // Initialize a vector with some integers
    vector<int> vec = {5, 2, 8, 1, 9, 3};

    // Display the original vector
    cout << "Original vector: ";
    for (int num : vec) {
        cout << num << " ";
    }
    cout << endl;

    // Sort the vector in ascending order
    sort(vec.begin(), vec.end());

    // Reverse the vector to get descending order
    reverse(vec.begin(), vec.end());

    // Display the sorted vector
    cout << "Sorted vector in descending order: ";
    for (int num : vec) {
        cout << num << " ";
    }
    return 0;
}

Output

Below is the output we get after sorting the elements in descending order.

Original vector: 5 2 8 1 9 3 
Sorted vector in descending order: 9 8 5 3 2 1 

Time Complexity: O(n log n) due to sort(), and O(n) for reverse(), resulting in overall O(n log n).

Space Complexity: O(1) since no extra space is used beyond the input vector.

Using greater<>() with sort()

C++ STL provides the greater<>() function in the functional header. We can pass it directly to the sort() function to sort a vector in descending order. The greater<>() function compares the elements in reverse order, so it sorts the vector in descending order without needing a custom lambda function.

Example

In this example, we use greater<int>() with the sort() function. This removes the need to write a custom comparator since greater<>() automatically sorts the elements in descending order.

#include <iostream>
#include <vector>
#include <algorithm>  // For sort()
#include <functional>  // For greater<>

using namespace std;

int main() {
    // Initialize a vector with some integers
    vector<int> vec = {5, 2, 8, 1, 9, 3};

    // Display the original vector
    cout << "Original vector: ";
    for (int num : vec) {
        cout << num << " ";
    }
    cout << endl;

    // Sort the vector in descending order using greater<>
    sort(vec.begin(), vec.end(), greater<int>());

    // Display the sorted vector
    cout << "Sorted vector in descending order: ";
    for (int num : vec) {
        cout << num << " ";
    }
    return 0;
}

Output

This shows the result of sorting the vector in descending order using greater<int>() with the sort() function.

Original vector: 5 2 8 1 9 3 
Sorted vector in descending order: 9 8 5 3 2 1 

Time Complexity: O(n log n) due to the sort() function.

Space Complexity: O(1) as no additional space is used beyond the input vector.

Conclusion

In this article, we covered three ways to sort a vector in descending order using C++ STL: using sort() with a custom comparator, reverse() after sorting, and greater<>() with sort(). All work well, but the third is the simplest and most efficient. Choose the method that best fits your needs.

Updated on: 2025-02-21T16:27:08+05:30

492 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements