TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm is_sorted() function

C++ algorithm is_sorted() function with c++ tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm is_sorted()

C++ Algorithm is_sorted() function returns true if the elements in the range [first, last) are sorted into ascending order.

The elements are compared using operator < for the first version, and comp for the second version.

Syntax

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

first: An forward iterator pointing to the first element in the range to be checked.

last: An random access iterator pointing to the past last element in the range to be checked.

comp: A user defined binary predicate function that accepts two arguments and returns true if the two arguments are in order and false otherwise. It follows the strict weak ordering to order the elements.

Return value

It returns true if the range [first, last) is sorted into ascending order, false otherwise.

Complexity

The Complexity is linear in the distance between first and last: compares pairs of elements until a mismatch is found.

Data races

The object in the range [first, last) are accessed.

Exceptions

This function throws an exception if either an element comparison, or an operation on iterator throws an exception.

Note: The invalid parameters cause an undefined behavior.

Example 1

Let's see the simple example to demonstrate the use of is_sorted():

#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:

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

Example 2

Let's see another simple 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:

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 above example shows the sequence of sorting and prints the elements until it is sorted.

Example 3

Let's see another simple example to check whether the elements are sorted or not:

#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:

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

Example 4

Let's see another simple 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:

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

Next TopicC++ Algorithm




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf