fork download
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <assert.h>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. void selectionSort(vector<string>& dictionary)
  11. {
  12. for (int i = 0; i < dictionary.size(); ++i)
  13. {
  14. int m = i;
  15. for (int j = i + 1; j < dictionary.size(); ++j)
  16. {
  17. if (dictionary[j] < dictionary[m])
  18. m = j;
  19. }
  20. if (m != i)
  21. swap(dictionary[i], dictionary[m]);
  22. }
  23. }
  24.  
  25. int main()
  26. {
  27. vector<string> data{ "hey", "ok", "so", "no", "okay" };
  28. vector<string> data2 = data;
  29. selectionSort(data);
  30. std::sort(data2.begin(), data2.end());
  31. assert(equal(data.begin(), data.end(), data2.begin())); // verify that your sort matches what std::sort does
  32. for (const auto s : data)
  33. cout << s << ' ';
  34. cout << endl;
  35. }
  36.  
Success #stdin #stdout 0s 16072KB
stdin
Standard input is empty
stdout
hey no ok okay so