fork download
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <vector>
  4. #include <limits>
  5. using namespace std;
  6.  
  7. bool Compare(const double &a, const double &b)
  8. {
  9. return a - b > 0.0;
  10. }
  11.  
  12. void InsertionSort(vector<double> &vect)
  13. {
  14. for (size_t i = 1; i < vect.size(); ++i)
  15. for (size_t j = i; j > 0 && Compare(vect[j], vect[j - 1]); --j)
  16. swap(vect[j], vect[j - 1]);
  17. }
  18.  
  19. int main()
  20. {
  21. vector <double> v = {1, 2, 3, 4, 5};
  22. InsertionSort(v);
  23. for (auto val : v)
  24. cout << val << " ";
  25. cout << endl;
  26.  
  27. double nan = numeric_limits<double>::quiet_NaN();
  28. v = {1, nan, 2, nan, 1};
  29. InsertionSort(v);
  30. for (auto val : v)
  31. cout << val << " ";
  32. cout << endl;
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 4384KB
stdin
Standard input is empty
stdout
5 4 3 2 1 
1 nan 2 nan 1