C++ Algorithm sort()30 Aug 2024 | 4 min read C++ Algorithm sort() function is used to sort the elements in the range [first, last) into ascending order. The elements are compared using operator < for the first version, and comp for the second version. SyntaxParameterfirst: An random access iterator pointing to the first element in the range to be sorted. last: An random access iterator pointing to the past last element in the range to be sorted. 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 valueNone ComplexityThe average of a sort complexity is N*log2 (N), where N = last - first. Data racesThe object in the range [first, last) are modified. ExceptionsThis function throws an exception if any of element comparisons, the element swaps 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 sort(): Output: Before sorting: 3 1 4 2 5 After sorting: 1 2 3 4 5 Example 2Let's see another simple example: Output: myvector contains: 12 26 32 33 45 53 71 80 Example 3Let's see another simple example: Output: Sorted list of functional programming languages - C# C++ Java Lisp Pascal Python Sql Reverse Sorted list of functional programming languages - Sql Python Pascal Lisp Java C++ C# Example 4Let's see another simple example: Output: Original random shuffle vector vec1 data: 4 10 11 15 14 5 13 1 6 9 3 7 8 2 0 12 Sorted vector vec1 data: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Re sorted (greater) vector vec1 data: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 User defined re sorted vector vec1 data: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Next TopicC++ Algorithm |
C++ Algorithm transform() C++ Algorithm transform() function is used in two different ways: 1.unary operation:- This method performs unary operation op on the elements in range [first1, last1] and stores the result in range starting from result. This transform() applies a function to each element of a range: 2.Binary...
5 min read
C++ Algorithm unique_copy() C++ Algorithm unique_copy() function is used to copy a sequence such as each duplicate consecutive element becomes a unique element. It will not alter the original range and copy the result into another container. The first version uses operator== to compare the elements and the second...
6 min read
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 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
C++ Algorithm unique() C++ Algorithm unique() function is used to transform a sequence in such a way that each duplicate consecutive element becomes a unique element. The first version uses operator== to compare the elements and the second version uses the given binary predicate pred. Syntax equality (1) template <class...
7 min read
C++ Algorithm Function adjacent_find() C++ Algorithm adjacent_find() function performs a search operation on the range [first, last] for the very first occurrence of two consecutive matching elements. If such elements are found then an iterator to the first element of the two is returned. Otherwise, the last...
1 min read
C++ Algorithm Functions move backward () The function is used for moving of elements in the backward order, it accepts three arguments and then moves the elements belonging to the range [first,last). The moving of elements begins in the reverse order with termination point at 'result'. Syntax template<class BidirectionalIterator1,...
2 min read
C++ Algorithm random_shuffle() C++ Algorithm random_shuffle() reorders the elements of a range by putting them at random places. The first version uses an internal random number generator and the second version uses a random number generator which is a special kind of function object that is explicitly...
4 min read
C++ Algorithm iter_swap() C++ Algorithm iter_swap() exchanges the elements pointed to by two iterators a and b. Syntax template <class ForwardIterator1, class ForwardIterator2> void iter_swap (ForwardIterator1 a, ForwardIterator2 b); Parameter a: One of the forward iterator whose value is to be exchanged. b: Second of the forward iterator whose value is...
7 min read
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
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