stable_sort() Function in C++

This C++ program demonstrates the stable_sort() algorithm. The program creates vector of objects of class Student which has attributes – name, section and group. The stable_sort algorithm first sorts the elements with name as the key and then the elements are sorted with their section as the key. The order of the elements with same keys is maintained.

Here is the source code of the C++ program which demonstrates the stable_sort() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Demonstrate the stable_sort() Algorithm
  3.  */
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8.  
  9. struct Student {
  10.     std::string name;
  11.     int sec;
  12.     char group;
  13. };
  14.  
  15. bool compBySec(Student a, Student b)
  16. {
  17.     return a.sec < b.sec;
  18. }
  19.  
  20. bool compByGroup(Student a, Student b)
  21. {
  22.     return a.group < b.group;
  23. }
  24.  
  25. bool compByName(Student a, Student b)
  26. {
  27.     return a.name < b.name;
  28. }
  29.  
  30. void print(const std::vector <Student>& v)
  31. {
  32.     std::cout << "Name  \tSec\tGroup" << std::endl;
  33.     for (unsigned int i = 0; i < v.size(); i++)
  34.     {   
  35.         std::cout << v[i].name << "\t" << v[i].sec<< "\t"
  36.                   << v[i].group << std::endl;
  37.     }
  38. }
  39.  
  40. int main()
  41. {
  42.     std::vector <Student> Students;
  43.     std::string name[] = {"Andrew", "Battle", "Chen  ", "Fox   ",
  44.                           "Furia ", "Gazsi ", "Kanaga", "Rohde "};
  45.     int sec[] = {3, 4, 3, 3, 1, 4, 3, 2};
  46.     int group[] = {'A', 'C', 'A', 'A', 'A', 'B', 'B', 'A'};
  47.  
  48.     for (int i = 0; i < 8; i++)
  49.     {
  50.         Student p;   
  51.         p.name =  name[i];
  52.         p.sec = sec[i];
  53.         p.group = group[i];
  54.         Students.push_back(p);
  55.     }
  56.     std::stable_sort(Students.begin(), Students.end(), compByName);
  57.     std::cout << "Stable Sort by name" << std::endl;
  58.     print(Students);
  59.     std::cout << std::endl;
  60.     std::stable_sort(Students.begin(), Students.end(), compBySec);
  61.     std::cout << "Stable Sort by section" << std::endl;
  62.     print(Students);
  63. }

$ a.out
Stable Sort by name
Name    Sec Group
Andrew  3   A
Battle  4   C
Chen    3   A
Fox     3   A
Furia   1   A
Gazsi   4   B
Kanaga  3   B
Rohde   2   A
 
Stable Sort by section
Name    Sec Group
Furia   1   A
Rohde   2   A
Andrew  3   A
Chen    3   A
Fox     3   A
Kanaga  3   B
Battle  4   C
Gazsi   4   B

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.