C++ Algorithm nth_element()30 Aug 2024 | 5 min read C++ Algorithm nth_element() function is used to sort the elements between the first and nth element in ascending order and element between nth and last are not sorted. However, no element in between nth and last is smaller than an element between first and nth element. The elements are compared using operator < for the first version, and comp for the second version. SyntaxParameterfirst: A random access iterator pointing to the first element in the range to be used. last: A random access iterator pointing to the past last element in the range to be used. 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. nth: A random access iterator addressing the position in the range[first, last) that will contain the sorted element. Return valueNone ComplexityOn average, complexity is linear in the distance between first and last: compares elements and possible swaps them, until the elements are properly rearranged. Data racesThe object in the range [first, last) are altered. ExceptionsThis function throws an exception if any of element comparison, element swap, 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 nth_element(): Output: Before: 1 3 6 1 2 4 7 0 After: 2 0 1 1 3 4 7 6 Example 2Let's see another simple example: Output: Elements are: 5 6 4 3 2 6 7 9 3 The median is 5 The second largest element is 7 Example 3Let's see another simple example: Output: myvector contains: 5 2 3 1 4 6 7 8 9 Example 4Let's see another simple example: Output: Original vector: v1 = ( 0 3 6 9 12 15 1 4 7 10 13 16 2 5 8 11 14 17 ) Position 3 partitioned vector: v1 = ( 1 0 2 3 8 5 9 4 7 6 10 16 13 15 12 11 14 17 ) Position 4 partitioned (greater) vector: v1 = ( 16 17 14 15 13 12 11 9 7 8 10 6 1 4 5 3 2 0 ) Shuffled vector: v1 = ( 13 10 6 3 5 2 0 17 11 8 15 9 7 14 16 1 12 4 ) Position 5 partitioned (UDgreater) vector: v1 = ( 14 17 15 16 13 12 10 11 9 8 0 2 7 5 3 1 6 4 ) Next TopicC++ Algorithm |
algorithm is_sorted_until() function
C++ Algorithm is_sorted_until() C++ Algorithm is_sorted_until() function is used to find first unsorted element in the range. It means it returns an iterator to the first element in the range [first, last) which does not follow an ascending order. The elements are compared using operator < for...
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 none_of() function
C++ Algorithm Functions none_of() C++ Algorithm none_of() function returns a true value if the value of 'pred' argument is false. The value should be false for all elements in the range [first, last). Syntax template <class InputIterator, class UnaryPredicate> bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first : It specifies...
2 min read
algorithm move() function
C++ Algorithm Function move () C++ Algorithm move()function is used for moving the elements. It accepts three arguments and then moves the elements belonging to the range [first,last) into a range that starts with 'result'. Syntax template<class InputIterator, class OutputIterator> OutputIterator move(InputIterator first, InputIterator last, OutputIterator result); Parameter first: It is...
2 min read
algorithm is_sorted() function
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> ...
4 min read
algorithm inplace_merge() function
C++ Algorithm inplace_merge() C++ Algorithm inplace_merge() function is used to merge two consecutive sorted ranges [first, last) and [middle, last) into one sorted range [first, last). Elements are compared using operator < for the first version or using the given binary comparison function comp for the second version. Syntax default...
5 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 copy() function
C++ Algorithm Function copy() C++ Algorithm copy() function is used to copy all the elements of the container [first,last] into a different container starting from result. Syntax template<class InputIterator, class OutputIterator>OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result); Parameter first: It is an input iterator to the first element of the range,...
1 min read
algorithm any_of() function
C++ Algorithm Function any_of() C++ Algorithm any_of() function tests the value of 'pred' for every element in the range, if for any element the value of pred is true, then the function returns true else return false. Syntax template <class InputIteratir, class UnaryPredicate> bool any_of (InputIterator first, InputIterator last, UnaryPredicate...
1 min read
C++ Algorithm next_permutation () Function
C++ Algorithm _permutation () C++ Algorithm _permutation() function is used to reorder the elements in the range [first, last) into the lexicographically greater permutation. A permutation is specified as each of several possible ways in which a set or number of things can be ordered or...
6 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