sort() in C++ STL

Last Updated : 31 Aug, 2026

The sort() function in C++ STL is used to sort elements within a specified range. By default, it sorts the elements in ascending order using the < operator.

  • Works with random-access iterators, such as those of arrays, vectors, and deques.
  • Supports custom comparison functions for different sorting orders.
C++
#include <iostream>
using namespace std;

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

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

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

Output
1 2 3 4 5 

Syntax

sort(first, last);

Parameters

  • first: Iterator to the beginning of the range to be sorted.
  • last: Iterator to the element just after the end of the range.

Variations and Usage of sort() in C++ STL

The sort() function can be used in different ways to sort complete or partial ranges and to define custom sorting criteria.

1. Sort the Entire Range

By default, sort() arranges the elements in ascending order.

C++
#include <algorithm>
#include <iostream>
using namespace std;

int main(){
    int arr[5] = {5, 3, 2, 1, 4};
    int n = sizeof(arr) / sizeof(arr[0]);

    // Sort array (by default in ascending order)
    sort(arr, arr + n);

    for (int i : arr)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 

2. Sort a specific range

sort() can be used to sort only a specific portion of an array or container.

C++
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
    int arr[] = {10, 5, 8, 1, 7, 3};
    int n = sizeof(arr) / sizeof(arr[0]);

    // Sort elements from index 1 to 4
    sort(arr + 1, arr + 5);

    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";

    return 0;
}

Output
10 1 5 7 8 3 

Explanation: sort(arr + 1, arr + 5) sorts the elements from index 1 to 4. The element at arr[5] is not included because the second iterator represents the position just after the range.

3. Sort Array in Descending Order

A custom comparator such as greater<int>() can be passed to sort() to arrange elements in descending order.

C++
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
    int arr[5] = {5, 3, 2, 1, 4};
    int n = sizeof(arr) / sizeof(arr[0]);

    // Sort array in descending order
    sort(arr, arr + n, greater<int>());

    for (int i : arr)
        cout << i << " ";

    return 0;
}

Output
5 4 3 2 1 

Explanation: sort(arr, arr + n, greater<int>()); sorts the entire array in descending order.

4. Sort Vector of User Defined Type

For user-defined types, a custom comparator can be provided to specify how the objects should be ordered.

C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct Point{
    int x;
    int y;
};

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

    // Sort points by x value (ascending)
    sort(points.begin(), points.end(), [](const Point &a, const Point &b) 
    { return a.x < b.x; });
    
    for (auto p : points)
        cout << "(" << p.x << "," << p.y << ") ";

    return 0;
}
Try It Yourself
redirect icon

Output
(1,2) (3,5) (4,1) 

Explanation:

  • Defines a Point structure with x and y values.
  • Creates a vector of Point objects.
  • Uses a lambda comparator to sort points by x in ascending order.
  • Prints the sorted points in (x, y) format.

Working of sort() Function

The sort() function is implemented using the Introsort (Intro Sort) algorithm, which is a hybrid sorting technique.

  1. Start with Quick Sort: The algorithm initially uses Quick Sort because it is fast and efficient for average cases.
  2. Monitor Recursion Depth: While performing Quick Sort, the algorithm keeps track of the recursion depth to avoid worst-case performance.
  3. Switch to Heap Sort if Needed: If the recursion depth exceeds a certain limit, it switches to Heap Sort to guarantee O(n log n) performance.
  4. Use Insertion Sort for Small Arrays: For very small subarrays, Insertion Sort is used as it performs better on small data sets.
  5. Final Sorted Output: By combining these techniques, sort() ensures both high performance and worst-case safety.
Comment