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

Algorithm Is Sorted Until Function

BLUF: Mastering Algorithm Is Sorted Until 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 Until Function

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

The C++ function issorteduntil is employed to identify the initial unsorted element within a specified range. This function locates and returns an iterator pointing to the first element in the range [first, last) that does not adhere to the ascending order.

The elements are assessed by employing the < operator in the initial version, and with comp in the subsequent version.

Syntax

Example

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

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

Parameter

An initial iterator pointing to the first element within the range that needs to be examined.

last : A random access iterator pointing to the element before the last one in the range that needs to be examined.

comp : A custom binary predicate function that takes two parameters and evaluates to true if the two parameters are in sequence, otherwise returning false. This function adheres to the strict weak ordering principle to arrange the elements.

Return value

It provides an iterator to the initial element if the range is disordered and provides the last element if the complete range is ordered.

Complexity

The linear complexity exists between the initial and final elements: the function "comp" is called for each element until a disparity is encountered.

Data races

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

Exceptions

This function will raise an exception if either comp or an operation on the iterator results in an exception.

Please note that invalid parameters cause an undefined behavior.

Example 1

Let's examine a basic example to showcase the functionality of the issorteduntil method:

Example

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

using namespace std;

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

  cout << boolalpha;
  cout << "Before: is it sorted? "
            << (is_sorted_until(v.begin(), v.end()) == v.end()) << endl;

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

  cout << "After: is it sorted? "
            << (is_sorted_until(v.begin(), v.end()) == v.end()) << endl;
            
  return 0;
}

Output:

Output

Before: is it sorted? false
After: is it sorted? true

Example 2

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 = {'A', 'b', 'C', 'd', 'E'};

   auto it = is_sorted_until(v.begin(), v.end());

   cout << "First unsorted element = " << *it << endl;

   it = is_sorted_until(v.begin(), v.end(), ignore_case);

   if (it == end(v))
      cout << "Entire vector is sorted." << endl;

   return 0;
}

Output:

Output

First unsorted element = C
Entire vector is sorted.

Example 3

Let's see another simple example:

Example

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

using namespace std;

int main(void) {
   vector<int> v = {1, 2, 5, 3, 4};
   auto it = is_sorted_until(v.begin(), v.end());

   cout << "First unsorted element = " << *it << endl;

   v[3] = 4;

   it = is_sorted_until(v.begin(), v.end());

   if (it == end(v))
      cout << "Entire vector is sorted." << endl;

   return 0;
}

Output:

Output

First unsorted element = 3

Example 4

Let's see another simple example:

Example

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

using namespace std;

int main () {
  array<int,4> foo {2,4,1,3};
  array<int,4>::iterator it;

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

    // print range:
    cout << "foo:";
    for (int& x:foo) cout << ' ' << x;
    it=is_sorted_until(foo.begin(),foo.end());
    cout << " (" << (it-foo.begin()) << " elements sorted)\n";

  } while (it!=foo.end());

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

  return 0;
}

Output:

Output

foo: 2 3 4 1 (3 elements sorted)
foo: 2 3 1 4 (2 elements sorted)
foo: 2 1 4 3 (1 elements sorted)
foo: 2 1 3 4 (1 elements sorted)
foo: 1 4 3 2 (2 elements sorted)
foo: 1 4 2 3 (2 elements sorted)
foo: 1 3 4 2 (3 elements sorted)
foo: 1 3 2 4 (2 elements sorted)
foo: 1 2 4 3 (3 elements sorted)
foo: 1 2 3 4 (4 elements sorted)
the range is sorted!

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