stable_sort() in C++ STL


The stable_sort method of STL first sorts the components with name as the key in ascending order and afterward the components are arranged with their segment as the key. Moreover, The stable_sort() calculation is viewed as steady in light of the fact that the overall request of comparable components is kept up. Here is the source code of the C++ program which exhibits the stable_sort() calculation demonstrated as follows;

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   int arr[] = { 11, 15, 18, 19, 16, 17, 13, 20, 14, 12, 10 };
   int n = sizeof(arr) / sizeof(arr[0]);
   stable_sort(arr, arr + n);
   cout << "Array after sorting is =";
   for (int i = 0; i < n; ++i)
      cout << arr[i] << " ";
   return 0;
}

Output

This C++ program yields the following array to be sorted in ascending order as follows;

Array after sorting is= 10 11 12 13 14 15 16 17 18 19 20

Updated on: 23-Dec-2019

144 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements