###  [ [ edit ](/mwiki/index.php?title=cpp/algorithm&action=edit&section=15
"Edit section: Sorting and related operations") ]  Sorting and related
operations

####  [ [ edit ](/mwiki/index.php?title=cpp/algorithm&action=edit&section=16
"Edit section: Requirements") ]  Requirements

Some algorithms require the sequence represented by the arguments to be
“sorted” or “partitioned”. The behavior is undefined if the requirement is not
met.

A sequence is _sorted with respect to a comparator comp  _ if for every iterator  iter  pointing to the sequence and every non-negative integer  n  such that  iter  \+  n  [1]  is a [ valid iterator ](/w/cpp/iterator#Dereferenceability_and_validity "cpp/iterator") pointing to an element of the sequence,  comp  (  *  (  iter  \+  n  )  ,  *  iter  )  ==  false  [1]  .  |  (until C++20)   
---|---  
A sequence is _sorted with respect to comp  and  proj  _ for a comparator
comp  and projection  proj  if for every iterator  iter  pointing to the
sequence and every non-negative integer  n  such that  iter  \+  n  [1]  is a
valid iterator pointing to an element of the sequence,  bool  (  [ std::
invoke  ](http://en.cppreference.com/w/cpp/utility/functional/invoke) (  comp,
[ std::  invoke  ](http://en.cppreference.com/w/cpp/utility/functional/invoke)
(  proj,  *  (  iter  \+  n  )  )  ,  
[ std::  invoke  ](http://en.cppreference.com/w/cpp/utility/functional/invoke) (  proj,  *  iter  )  )  )  [1]  is  false  .  A sequence is _sorted with respect to a comparator comp  _ if the sequence is sorted with respect to  comp  and  [ std::  identity  ](http://en.cppreference.com/w/cpp/utility/functional/identity) {  }  (the identity projection).  |  (since C++20)   
  
A sequence ` [ ` start  ` , ` finish  ` ) ` is _partitioned with respect to an
expression f  (  e  )  _ if there exists an integer  n  such that for all  i
in ` [ ` ​  0  ​  ` , ` [ std::  distance
](http://en.cppreference.com/w/cpp/iterator/distance) (  start, finish  )  ` )
` ,  f  (  *  (  start  \+  i  )  )  [1]  is  true  if and only if  i  < n  .

  1. ↑  1.0  1.1  1.2  1.3  1.4  iter  \+  n  simply means “the result of  iter  being incremented  n  times”, regardless of whether  iter  is a random access iterator. 

####  [ [ edit ](/mwiki/index.php?title=cpp/algorithm&action=edit&section=17
"Edit section: Partitioning operations") ]  Partitioning operations

Defined in header ` [ <algorithm> ](/w/cpp/header/algorithm
"cpp/header/algorithm") `  
---  
[ is_partitioned  ](/w/cpp/algorithm/is_partitioned "cpp/algorithm/is partitioned") (C++11)  |  determines if the range is partitioned by the given predicate   
(function template)  [ [edit]
](https://en.cppreference.com/mwiki/index.php?title=Template:cpp/algorithm/dsc_is_partitioned&action=edit)  
[ ranges::is_partitioned  ](/w/cpp/algorithm/ranges/is_partitioned "cpp/algorithm/ranges/is partitioned") (C++20)  |  determines if the range is partitioned by the given predicate   
(niebloid)  [ [edit]
](https://en.cppreference.com/mwiki/index.php?title=Template:cpp/algorithm/ranges/dsc_is_partitioned&action=edit)  
[ partition  ](/w/cpp/algorithm/partition "cpp/algorithm/partition") |  divides a range of elements into two groups   
(function template)  [ [edit]
](https://en.cppreference.com/mwiki/index.php?title=Template:cpp/algorithm/dsc_partition&action=edit)  
[ ranges::partition  ](/w/cpp/algorithm/ranges/partition "cpp/algorithm/ranges/partition") (C++20)  |  divides a range of elements into two groups   
(niebloid)  [ [edit]
](https://en.cppreference.com/mwiki/index.php?title=Template:cpp/algorithm/ranges/dsc_partition&action=edit)  
[ partition_copy  ](/w/cpp/algorithm/partition_copy "cpp/algorithm/partition copy") (C++11)  |  copies a range dividing the elements into two groups   
(function template)  [ [edit]
](https://en.cppreference.com/mwiki/index.php?title=Template:cpp/algorithm/dsc_partition_copy&action=edit)  
[ ranges::partition_copy  ](/w/cpp/algorithm/ranges/partition_copy "cpp/