In C++, is_sorted() is a built-in function used to check whether the elements in the given range are sorted or not in ascending order by default. You can also provide a custom comparator to check for descending order or any other sorting criteria.
For example: Let's create a vector and check if the elements are sorted using is_sorted() method.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 5, 6, 8, 9};
if (is_sorted(v.begin(), v.end()))
cout << "Sorted";
else
cout << "Not Sorted";
return 0;
}
Output
Sorted
Syntax
is_sorted() is defined inside <algorithm> header file.
is_sorted(first, last, comp);
Parameters:
- first: Iterator to the first element of given range.
- last: Iterator to the element just after the last element of given range.
- comp(optional): It is a custom comparison function used to change the sorting order to be checked. By default, it uses the < operator which check if the range is sorted in ascending order.
Return Value: Returns true, if the range is sorted otherwise it returns false.
Examples of is_sorted()
The following examples demonstrates the use of is_sorted() method in different scenario:
Check if an Array is Sorted in Ascending Order
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {4, 7, 8, 9};
int n = sizeof(arr)/sizeof(arr[0]);
// Check whether array is sorted in ascending
// order
if (is_sorted(arr, arr + n))
cout << "Sorted";
else
cout << "Not Sorted";
return 0;
}
Output
Given Array is Sorted
Check if Part of a Vector is Sorted
We can check if a given vector or array is sorted within a specified range.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {11, 25, 6, 8, 9, 10};
if (is_sorted(v.begin()+2, v.end()))
cout << "Sorted";
else
cout << "Not Sorted";
return 0;
}
Output
Sorted
Explanation: is_sorted(v.begin()+2, v.end()) check if the vector is sorted from index '2' till the end.
Check if a List is Sorted in Descending Order
#include <bits/stdc++.h>
using namespace std;
// Comparator function
bool comp(int a, int b) {
return a > b;
}
int main() {
list<int> l = {9, 7, 6, 3};
// Check vector Sorted in descending order
if (is_sorted(l.begin(), l.end(), comp))
cout << "Sorted";
else
cout << "Not Sorted";
return 0;
}
Output
Given Vector is Sorted
Check if Vector of String is in Lexicographically Ordered
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<string> v = {"hi", "geeks", "welcome", "to" "geeksforgeeks"};
// Check vector sorted in user
// defined order or not
if (is_sorted(v.begin(), v.end()))
cout << "Sorted";
else
cout << "Not Sorted";
return 0;
}
Output
Given Vector is Sorted
Check if Set is Sorted in Descending Order
#include <bits/stdc++.h>
using namespace std;
// Comparator function
bool comp(int a, int b) {
return a > b;
}
int main() {
set<int> s = {9, 7, 6, 3};
// Check vector Sorted in descending order
if (is_sorted(s.begin(), s.end(), comp))
cout << "Sorted";
else
cout << "Not Sorted";
return 0;
}
Output
Not Sorted
Explanation: The set is already sorted in ascending order. We have used is_sorted() with custom comparator to check if the set is in descending order.