We have discussed qsort() in C. C++ STL provides a similar function sort that sorts a vector or array (items with random access). Below is a simple program to show working of sort().
Array after sorting using default sort is : 0 1 2 3 4 5 6 7 8 9
So by default, sort() sorts an array in ascending order.
How to sort in descending order?
sort() takes a third parameter that is used to specify the order in which elements are to be sorted. We can pass “greater()” function to sort in descending order. This function does comparison in a way that puts greater element before.
Array after sorting : 9 8 7 6 5 4 3 2 1 0
How to sort in particular order?
We can also write our own comparator function and pass it as a third parameter.
Intervals sorted by start time : [1,9] [2,4] [4,7] [6,8]
This article is contributed by Shubham Agrawal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- k-th missing element in an unsorted array
- Minimum operations to make frequency of all characters equal K
- Methods to concatenate string in C/C++ with Examples
- Bitwise AND of N binary strings
- Convex Hull | Monotone chain algorithm
- Maximum number of segments that can contain the given points
- Compiling with g++
- Sort the numbers according to their sum of digits
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Print k different sorted permutations of a given array
- Merge K sorted arrays of different sizes | ( Divide and Conquer Approach )
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach )
- Why learning C Programming is a must?
Improved By : rahuku