Sorting a Vector in C++

Last Updated : 30 May, 2026

Sorting a vector in C++ means arranging its elements in a specific order such as ascending, descending, or user-defined order. The C++ STL provides efficient sorting functions and comparators to sort vectors easily.

  • The sort() function is commonly used for efficient vector sorting in C++
  • Vectors can be sorted in ascending, descending, or custom-defined order
  • C++ also provides stable_sort() to preserve the order of equal elements

Sorting Methods for Vectors

The STL provides different sorting methods to arrange vector elements in ascending, descending, custom, or stable order.

Sorting in Ascending Order

The sort() function from the <algorithm> header file is used to sort elements in ascending order by default. It has a time complexity of O(n log n) and a space complexity of O(1).

  • sort() is a built-in STL algorithm used for efficient sorting
  • By default, it arranges elements from smallest to largest
  • It sorts the elements within the range specified by iterators
C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v = {1, 4, 3, 2, 5};

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

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 

Explanation

In this program, the sort() function rearranges the vector elements in ascending order.

  • v.begin() specifies the starting position of the vector
  • v.end() specifies the position after the last element
  • The elements are automatically arranged from smallest to largest

Sorting in Descending Order

C++ provides a built-in comparator greater<int>() that can be passed to sort() to arrange elements in descending order. It has a time complexity of O(n log n) and a space complexity of O(1).

  • greater<int>() is a predefined comparator available in the STL
  • It instructs sort() to place larger elements before smaller ones
  • It provides an easy way to sort elements in descending order
C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v = {5, 2, 4, 1, 3};

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

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
5 4 3 2 1 

Explanation

In this program, the sort() function uses the greater<int>() comparator to sort the vector in descending order.

  • v.begin() specifies the starting position of the sorting range
  • v.end() specifies the position after the last element of the vector
  • greater<int>() changes the default sorting order from ascending to descending
  • The elements are sorted from largest to smallest, resulting in 5 4 3 2 1

Sorting in Custom Order

A custom comparator in C++ is a user-defined function or callable object that specifies how two elements should be compared during sorting. It allows sorting elements in a custom order instead of the default ascending order used by sort(). The sorting operation has a time complexity of O(n log n) and a space complexity of O(1).

  • A custom comparator defines the sorting criteria for elements
  • It is passed as the third argument to the sort() function
  • It allows sorting based on user-defined conditions
C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// Comparator function for descending order
bool comp(int a, int b)
{
    return a > b;
}

int main()
{
    vector<int> v = {5, 2, 4, 1, 3};

    // Sort vector in descending order
    sort(v.begin(), v.end(), comp);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
5 4 3 2 1 

Explanation

In this program, the sort() function uses the custom comparator comp() to determine the order of elements.

  • The comparator returns true when the first element is greater than the second element
  • sort() uses this comparison rule while arranging the vector elements
  • v.begin() and v.end() define the range of elements to be sorted
  • The elements are sorted from largest to smallest, resulting in 5 4 3 2 1

Stable Sorting

The stable_sort() function from the <algorithm> header file is similar to sort(), but it preserves the relative order of equal elements after sorting. It has a time complexity of O(n log n) and a space complexity of O(n).

  • stable_sort() arranges elements in ascending order by default
  • It maintains the original order of elements that compare equal
  • It is useful when the relative order of equal elements must be preserved
C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v = {1, 4, 3, 2, 5};

    // Sort vector in ascending order
    stable_sort(v.begin(), v.end());

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 

Explanation

In this program, the stable_sort() function sorts the vector elements in ascending order.

  • v.begin() specifies the starting position of the sorting range
  • v.end() specifies the position after the last element of the vector
  • The elements are arranged from smallest to largest, resulting in 1 2 3 4 5
  • If two or more elements are equal, their original relative order is preserved after sorting
Comment