Need of stable sort | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need of stable sort

Hi If i am not wrong, stable sort maintains relative order. Refer code below: id for ketan and Ketan is same... so when sorted, the one which was inserted first (Ketan) is maintained after sorting. id for abc and Abc is same... so when sorted, the one which was inserted first (abc) is maintained after sorting. So, if sort is giving this output, what is need of stable_sort ? https://code.sololearn.com/cbAsACc779sz/?ref=app

3rd Dec 2022, 5:43 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 2
The difference is shown when more items are added. https://code.sololearn.com/culr3G12ibgB/?ref=app
3rd Dec 2022, 9:20 AM
Lochard
Lochard - avatar
+ 4
Quoted from https://en.cppreference.com/w/cpp/algorithm/stable_sort "The order of equivalent elements is guaranteed to be preserved" This is different with std::sort Description, as quoted from https://en.cppreference.com/w/cpp/algorithm/sort "The order of equivalent elements is not guaranteed to be preserved"
3rd Dec 2022, 10:30 AM
Ipang