Algorithm Is Sorted Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm Is Sorted Function

Algorithm Is Sorted Function

BLUF: Mastering Algorithm Is Sorted Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Algorithm Is Sorted Function

C++ is renowned for its efficiency. Learn how Algorithm Is Sorted Function enables low-level control and high-performance computing in the tutorial below.

The is_sorted function in C++ returns true if the elements within the range [first, last) are arranged in ascending order.

The elements are evaluated by using the < operator in the initial version, and by utilizing comp in the subsequent version.

Syntax

Example

default (1)	template <class ForwardIterator>
                    bool is_sorted (ForwardIterator first, ForwardIterator last);

custom (2)	 template <class ForwardIterator, class Compare>
 	       bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);

Parameter

An initial iterator pointing to the first item within the range that requires validation.

last : A random access iterator indicating the element following the last one in the range to be examined.

A custom binary predicate function named comp is created by the user. This function takes two arguments and evaluates to true if the arguments are in a specific order, otherwise returning false. It adheres to the strict weak ordering principle to arrange the elements.

Return value

It returns true if the range [first, last) is arranged in ascending order, or false if it is not.

Complexity

The time complexity grows linearly with the distance between the first and last elements. It compares pairs of elements until a difference is detected.

Data races

The elements within the range [first, last) are being accessed.

Exceptions

This function will raise an exception if an exception is thrown during either an element comparison or an iterator operation.

Note: The invalid parameters cause an undefined behavior.

Example 1

Let's explore a basic example to showcase the functionality of the is_sorted method:

Example

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  vector<int> v = {3, 1, 4, 2, 5};

  cout << std::boolalpha;
  cout << "before sorting: is sorted? " << is_sorted(v.begin(), v.end()) << endl;

  sort(v.begin(), v.end());

  cout << "after sorting : is sorted? " << is_sorted(v.begin(), v.end()) << endl;
  
  return 0;
}

Output:

Output

before sorting: is sorted? false
after sorting : is sorted? True

Example 2

Let's see another simple example:

Example

#include <iostream>     // std::cout
#include <algorithm>    // std::is_sorted, std::prev_permutation
#include <array>        // std::array

using namespace std;

int main () {
  array<int,5> a {2,4,1,3,5};

  do {
    // try a new permutation:
    prev_permutation(a.begin(),a.end());

    // print range:
    cout << "a:";
    for (int& x:a) cout << ' ' << x;
    cout << '\n';

  } while (!is_sorted(a.begin(),a.end()));

  cout << "the range is sorted!\n";

  return 0;
}

Output:

Output

a: 2 3 5 4 1
a: 2 3 5 1 4
a: 2 3 4 5 1
a: 2 3 4 1 5
a: 2 3 1 5 4
a: 2 3 1 4 5
a: 2 1 5 4 3
a: 2 1 5 3 4
a: 2 1 4 5 3
a: 2 1 4 3 5
a: 2 1 3 5 4
a: 2 1 3 4 5
a: 1 5 4 3 2
a: 1 5 4 2 3
a: 1 5 3 4 2
a: 1 5 3 2 4
a: 1 5 2 4 3
a: 1 5 2 3 4
a: 1 4 5 3 2
a: 1 4 5 2 3
a: 1 4 3 5 2
a: 1 4 3 2 5
a: 1 4 2 5 3
a: 1 4 2 3 5
a: 1 3 5 4 2
a: 1 3 5 2 4
a: 1 3 4 5 2
a: 1 3 4 2 5
a: 1 3 2 5 4
a: 1 3 2 4 5
a: 1 2 5 4 3
a: 1 2 5 3 4
a: 1 2 4 5 3
a: 1 2 4 3 5
a: 1 2 3 5 4
a: 1 2 3 4 5
the range is sorted!

The preceding example illustrates the process of sorting a sequence and displays the elements until the sequence is completely sorted.

Example 3

Let's examine another straightforward example to verify if the items are arranged in order or not:

Example

#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;

int main() 
{
    int digits[] = {3, 1, 4, 1, 5};
 
    for (auto i : digits) cout << i << ' ';
    cout << ": is_sorted: " << boolalpha
              << is_sorted(begin(digits), end(digits)) << '\n';
 
    sort(begin(digits), end(digits));
 
    for (auto i : digits) cout << i << ' ';
    cout << ": is_sorted: "
              << is_sorted(begin(digits), end(digits)) << '\n';
              
    return 0;
}

Output:

Output

3 1 4 1 5 : is_sorted: false
1 1 3 4 5 : is_sorted: true

Example 4

Let's see another simple example:

Example

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool ignore_case(char a, char b) {
   return (tolower(a) == tolower(b));
}

int main(void) {
   vector<char> v = {'D', 'b', 'C', 'p', 'N'};
   bool result;

   result = is_sorted(v.begin(), v.end());

   if (result == false)
      cout << "Vector elements are not sorted in ascending order." << endl;

   result = is_sorted(v.begin(), v.end(), ignore_case);

   if (result == true)
      cout << "Vector elements are sorted in ascending order." << endl;

   return 0;
}

Output:

Output

Vector elements are not sorted in ascending order.
Vector elements are sorted in ascending order.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience