Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. void insertionSort(vector<double> &data)
  7. {
  8. for (int j = 1; j < data.size(); j++)
  9. {
  10. double key = data.at(j);
  11. int i = j - 1;
  12. while (i >= 0 && data.at(i) > key)
  13. {
  14. data.at(i+1) = data.at(i);
  15. i--;
  16. }
  17. data.at(i+1) = key;
  18. }
  19. }
  20.  
  21. int main(){
  22. ifstream inputFile("input.txt");
  23. double inputValue;
  24. vector<double> data;
  25.  
  26. cout << "Data:" << endl;
  27. while (inputFile >> inputValue)
  28. {
  29. data.insert(data.begin(), inputValue);
  30. cout << inputValue << " ";
  31. } // input
  32. cout << endl;
  33.  
  34. insertionSort(data);
  35.  
  36. cout << "Sorted data:" << endl;
  37. for (auto c : data)
  38. {
  39. cout << c << " ";
  40. } //output
  41. cout << endl;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement