The sort() function is used to sort elements in a container or array. It provides a simple and efficient way to sort data in C++.
- Works on random-access iterators (arrays, vectors, deques).
- Default sort is ascending order.
#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 of sort()
The std::sort() function is defined inside the <algorithm> header file.
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
1. Sort the entire range (default ascending order)
#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
#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(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 array elements from index 1 to 4 while leaving the rest unchanged.
3. Sort Array in Descending Order
#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
#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
Output
(1,2) (3,5) (4,1)
Explanation:
- A Point structure is defined with two integers x and y.
- A vector of Point objects is created with some initial values.
- The sort() function is used with a lambda comparator to sort points based on x in ascending order.
- The lambda compares two points and returns true when a.x < b.x.
- After sorting, the points are printed 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.
Step-by-step working:
- Start with Quick Sort: The algorithm initially uses Quick Sort because it is fast and efficient for average cases.
- Monitor Recursion Depth: While performing Quick Sort, the algorithm keeps track of the recursion depth to avoid worst-case performance.
- 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.
- Use Insertion Sort for Small Arrays: For very small subarrays, Insertion Sort is used as it performs better on small data sets.
- Final Sorted Output: By combining these techniques, sort() ensures both high performance and worst-case safety.