C++ Algorithm is_sorted()30 Aug 2024 | 4 min read 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. SyntaxParameterfirst: 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 valueIt returns true if the range [first, last) is sorted into ascending order, false otherwise. ComplexityThe Complexity is linear in the distance between first and last: compares pairs of elements until a mismatch is found. Data racesThe object in the range [first, last) are accessed. ExceptionsThis 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 1Let's see the simple example to demonstrate the use of is_sorted(): Output: before sorting: is sorted? false after sorting : is sorted? True Example 2Let's see another simple example: 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 3Let's see another simple example to check whether the elements are sorted or not: Output: 3 1 4 1 5 : is_sorted: false 1 1 3 4 5 : is_sorted: true Example 4Let's see another simple example: Output: Vector elements are not sorted in ascending order. Vector elements are sorted in ascending order. Next TopicC++ Algorithm |
algorithm binary_search() function
C++ Algorithm binary_search() C++ Algorithm binary_search() function is used check whether the element in the range [first, last) is equivalent to val (or a binary predicate) and false otherwise. The range [first, last) must satisfy all of the following conditions: Partitioned with respect to element < val or comp...
5 min read
algorithm partition_point() function
C++ Algorithm partition_point() C++ Algorithm partition_point() function is used to return the first element in the given range for which pred is not true. The elements are sorted in a way that the elements which satisfy the condition come before those who do not satisfy. Syntax template <class...
5 min read
algorithm is_permutation() function
C++ Algorithm Functions is_permutation() C++ Algorithm is_permutation() function compares the elements in both the containers and returns a true value if all the elements in both the containers are found to be matching even if in different order. The first range is from [first1, last1) and the...
1 min read
algorithm fill_n() function
C++ Algorithm fill_n() C++ Algorithm fill_n() function is used to assign a new value to a specified number of elements in a range beginning with a particular element. It means in fill_n(), we specify beginning position, number of elements to be filled and value to be filled. Syntax template <class...
4 min read
algorithm partition() function
C++ Algorithm partition() C++ Algorithm partition() function is used to make partition the elements on the basis of given predicate (condition) mentioned in its arguments. If the container is partitioned then this function returns true, else returns false. Syntax template <class BidirectionalIterator, class UnaryPredicate> BidirectionalIterator partition (BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate...
5 min read
algorithm reverse() function
C++ Algorithm reverse() C++ Algorithm reverse() function is used to reverse the order of the elements within a range [first, last). Syntax template <class BidirectionalIterator> void reverse (BidirectionalIterator first, BidirectionalIterator last); Note: BidirectionalIterator is an iterator which is used to access any elements of a container in both forward and...
4 min read
algorithm find_first_of() function
C++ Algorithm Function find_first_of() C++ Algorithm find_first_of() function compares the values stored in two containers i.e [first1, last1) and [first2, last2). If an element similar to the one in the range [first2, last2) is found in [first1, last1) then an iterator to that element is returned by...
2 min read
algorithm find_if() function
C++ Algorithm Function find_if() C++ Algorithm find_if() function returns the value of the first element in the range for which the pred value is true otherwise the last element of the range is given. Syntax template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first: It specifies...
2 min read
algorithm remove_if() function
C++ Algorithm remove_if() C++ Algorithm remove_if() function is used to eliminate all the elements that satisfy a predicate from a given range [first, last) without disturbing the order of the remaining elements. This function cannot alter the size of the container. It returns an iterator to the new end...
6 min read
algorithm swap_ranges() function
C++ Algorithm swap_ranges() C++ Algorithm swap_ranges() exchanges the elements in the range [first1, last2) with the elements present in the range starting from first2. In short we can say that, swap_ranges() swaps the elements of two sequence, that is each element at a position in the first sequence...
5 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India