Skip to content
Related Articles

Related Articles

Improve Article

std::is_sorted in C++

  • Difficulty Level : Medium
  • Last Updated : 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: 
 

  1. 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.
  1. 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.
  1.  

CPP




// CPP program to illustrate
// std::is_sorted
// without binary predicate
#include <iostream>
#include <algorithm>
 
// Driver Code
int 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;
}
  1. Output : 
     
Sorted in the range : 3
Not Sorted in the range : 4
  1. We have discussed other approaches here
     
  2. 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.
  1. 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
  1.  

CPP




// CPP program to illustrate
// std::is_sorted
// using binary predicate
#include <iostream>
#include <algorithm>
 
using namespace std;
 
// Binary predicate
bool 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 case
bool 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 code
int 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;
}
  1. Output : 
     
Not sorted
  1. 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.
 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :