Like std::sort(), stable_sort also sorts an array. The syntax is also same.
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?
Like sort(), stable_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 decreasing 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
When to prefer stable_sort over sort()?
Sometime we want to make sure that order of equal elements is same in sorted array as it was in original array. This can be useful if these values have associated other fields. For example, consider sorting students by marks, if two students have same marks, we may want to keep them in same order as they appear input. Please refer stability in sorting algorithms for details.
Consider following example. We have time intervals sorted by ending time and we want to sort by start time. Also, if two start times are same, we want to keep them sorted by end time. This is not guaranteed by sort().
Intervals sorted by start time : [1, 3] [1, 7] [2, 4] [2, 19]
sort() function usually uses Introsort. Therefore, sort() may preserve the physical order of semantically equivalent values but can’t be guaranteed.
stable_sort() function usually uses mergesort. Therefore, stable_sort() preserve the physical order of semantically equivalent values and its guaranteed.
For sort() it is O(n*log(n))
For stable_sort() it is O(n*log^2(n)) if additional memory linearly proportional to length is not available. If its available then O(n*log(n)).
- cin get() in C++ with Examples
- C program for Time Complexity plot of Bubble, Insertion and Selection Sort using Gnuplot
- Difference between Increment and Decrement Operators
- Sorting Algorithm Visualization : Merge Sort
- Map of Vectors in C++ STL with Examples
- Problem in comparing Floating point numbers and how to compare them correctly?
- exit(0) vs exit(1) in C/C++ with Examples
- Median of sliding window in an array
- Find the distance between two person after reconstruction of queue
- Find the minimum cost to cross the River
- Minimize the sum of differences of consecutive elements after removing exactly K elements
- Check if N rectangles of equal area can be formed from (4 * N) integers
- Minimum elements to be removed from the ends to make the array sorted
- Value to be subtracted from array elements to make sum of all elements equals K
- C++ program to print all Even and Odd numbers from 1 to N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mannprerak2