# stable_sort() in C++ STL

Like std::sort(), stable_sort also sorts an array. The syntax is also same.

`// C++ program to demonstrate default behaviour of ` `// sort() in STL. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `stable_sort(arr, arr + n); ` ` ` ` ` `cout << ` `"\nArray after sorting using "` ` ` `"default sort is : \n"` `; ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `cout << arr[i] << ` `" "` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output :

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.

`// C++ program to demonstrate descending order ` `// stable sort using greater<>(). ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `stable_sort(arr, arr + n, greater<` `int` `>()); ` ` ` ` ` `cout << ` `"Array after sorting : \n"` `; ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `cout << arr[i] << ` `" "` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

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().

`// A C++ program to demonstrate STL stable_sort() ` `// to sort intervals according to start time. ` `// Given intervals are sorted according to ` `// ending time ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// An interval has start time and end time ` `struct` `Interval { ` ` ` `int` `start, end; ` `}; ` ` ` `// Compares two intervals according to staring ` `// times. ` `bool` `compareInterval(Interval i1, Interval i2) ` `{ ` ` ` `return` `(i1.start < i2.start); ` `} ` ` ` `int` `main() ` `{ ` ` ` `// Given intervals are sorted according to end time ` ` ` `Interval arr[] = { {1, 3}, {2, 4}, {1, 7}, {2, 19} }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// sort the intervals in increasing order of ` ` ` `// start time such that the start time intervals ` ` ` `// appear in same order as in input. ` ` ` `stable_sort(arr, arr + n, compareInterval); ` ` ` ` ` `cout << ` `"Intervals sorted by start time : \n"` `; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `cout << ` `"["` `<< arr[i].start << ` `", "` `<< arr[i].end ` ` ` `<< ` `"] "` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Intervals sorted by start time : [1, 3] [1, 7] [2, 4] [2, 19]

**Implementation**

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.

**Time Complexity**

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)).

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- K most occurring strings
- Median of sliding window in an array | Set 2
- Minimum distance between any most frequent and least frequent element of an array
- Print all numbers in given range having digits in strictly increasing order
- Count of all pairs in an Array with minimum absolute difference
- Replace each element of Array with it's corresponding rank
- Check if a decreasing Array can be sorted using Triple cyclic shift
- Maximum distance between two points in coordinate plane using Rotating Caliper's Method
- Sort a Bitonic Array
- Quick Sort using Multi-threading
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- Split array into K subsets to maximize their sum of maximums and minimums
- Min steps to empty an Array by removing a pair each time with sum at most K
- Select K elements from an array whose maximum value is minimized
- Sorting boundary elements of a matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.