In C++, is_sorted() is a built-in function used to check whether the element of the given range is sorted or not in ascending order. In this article, we will learn about is_sorted() function in C++.
Let’s take a quick look at a simple example that illustrates is_sorted() method:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 5, 6, 8, 9};
// Checking if vector v is sorted or not
if (is_sorted(v.begin(), v.end()))
cout << "Sorted";
else
cout << "Not Sorted";
return 0;
}
OutputGiven Vector is Sorted
Explanation: By default, is_sorted() function checks whether the range is sorted or not in ascending order. As the vector v is sorted in ascending, the function returned true. Let's look at is_sorted() in detail.
This article covers the syntax, usage, and common examples of is_sorted() method in C++:
Syntax of is_sorted()
The is_sorted() is defined inside <algorithm> header file.
is_sorted(first, last);
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 in the given order.
- Returns false, if the range is not sorted in given order.
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
C++
#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;
}
OutputGiven Array is Sorted
Check if a List is Sorted in Descending Order
C++
#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;
}
OutputGiven Vector is Sorted
Explanation: As by default, is_sorted() checks for ascending order, we have to provide custom comparison function to check for descending order.
Check if Vector of String is in Lexicographically Ordered
C++
#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;
}
OutputGiven Vector is Sorted
Check if Set is Sorted in Descending Order
C++
#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;
}
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.
Similar Reads
std::is_sorted_until in C++ std::is_sorted_until is used to find out the first unsorted element in the range [first, last). It returns an iterator to the first unsorted element in the range, so all the elements in between first and the iterator returned are sorted.It can also be used to count the total no. of sorted elements i
3 min read
std::list::sort in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::sort() sort() function is used to sort the
2 min read
sort() in C++ STL In C++, sort() is a built-in function used to sort the given range in desired order. It provides a simple and efficient way to sort the data in C++, but it only works on data structures that provide random access to its elements such as vectors and arrays.Let's take a look at an example:C++#include
4 min read
std::partial_sort in C++ std::sort is used for sorting the elements present within a container. One of the variants of this is std::partial_sort , which is used for sorting not the entire range, but only a sub-part of it. It rearranges the elements in the range [first, last), in such a way that the elements before middle ar
7 min read
std::forward_list::sort() in C++ STL Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of
3 min read