std::is_sorted in C++

31 Aug, 2021
The C++ function std :: is_sorted checks if the elements in range [first, last] are sorted in ascending order. Elements are compared using < operator. There are two variants of std::is_sorted:  
Without using Binary predicate  
Without using Binary predicate  
bool is_sorted( ForwardIt first, ForwardIt last );
first, last : the range of elements to examine
Return value : 
true: if the elements are in non-decreasing order.
false: any element in increasing order.
Example : Given a container of size n, and a range between [0 ... n], write a program to check if it is sorted in ascending order or not. Equal values are allowed in array and two consecutive equal values are considered sorted.  
Example : Given a container of size n, and a range between [0 ... n], write a program to check if it is sorted in ascending order or not. Equal values are allowed in array and two consecutive equal values are considered sorted.  
Input : 2 5 9 4      /*Range = 3*/
Output : Sorted in given range.

Input : 3 5 1 9     /*Range = 3*/
Output : Not sorted in given range.
 
 
CPP
// CPP program to illustrate// std::is_sorted// without binary predicate#include <iostream>#include <algorithm> // Driver Codeint main(){    int A[] = { 10, 11, 15, 12 };     // Index 0 to 2    int range1 = 3;     // Index 0 to 3    int range2 = 4;     // Condition if container is sorted or not in range1    if (std::is_sorted(A, A + range1)) {        std::cout << "Sorted in the range : " << range1 << std::endl;    } else {        std::cout << "Not Sorted in the range : " << range1 << std::endl;    }     // Condition if container is sorted or not in range2    if (std::is_sorted(A, A + range2)) {        std::cout << "Sorted in the range : " << range2 << std::endl;    } else {        std::cout << "Not Sorted in the range : " << range2 << std::endl;    }    return 0;}
Output :  
Output :  
Sorted in the range : 3
Not Sorted in the range : 4
We have discussed other approaches here.  Using binary predicate  
We have discussed other approaches here.  
Using binary predicate  
bool is_sorted (ForwardIt first, ForwardIt last, Compare comp);
first, last : the range of elements to examine
comp : binary predicate
Return value : 
true: if the elements are in non-decreasing order.
false: any element in increasing order.
Example : Given a string composed of characters only. Check if the characters are in sorted order. Also, Ignore the cases while comparing, i.e. ‘f’ > ‘A’  
Example : Given a string composed of characters only. Check if the characters are in sorted order. Also, Ignore the cases while comparing, i.e. ‘f’ > ‘A’  
Input : AHZP
Output : Not Sorted

Input : Boy
Output : Sorted
 
 
CPP
// CPP program to illustrate// std::is_sorted// using binary predicate#include <iostream>#include <algorithm> using namespace std; // Binary predicatebool ignore_case(char a, char b){    // Converts both characters to lowercase and checks if a <= b    return (tolower(a) <= tolower(b));} // Function that checks if string is sorted while ignoring the casebool check_if_sorted(string str){    // Function call to is_sorted with binary predicate ignore_case    return is_sorted(str.begin(), str.end(), ignore_case);} // Driver codeint main(){    // String which is to be checked    string str = "tOY";     // Function returned true, string is sorted    if (check_if_sorted(str)) {        cout << "Sorted";    }    // Function returned false, string not sorted    else {        cout << "Not sorted";    }     return 0;}
Output :  
Output :  
Not sorted
Time Complexity: The Complexity is linear in the distance between first and last: compares pairs of elements until a mismatch is found.
Time Complexity: The Complexity is linear in the distance between first and last: compares pairs of elements until a mismatch is found.
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
sarveshkumar10
swayameron
cpp-algorithm-library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.