Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #include "Sorting.h"
  2.  
  3. vector<int> Sorting::mergesort(vector<int> input)
  4. {
  5. int n = input.size();
  6. vector<int> low;
  7. vector<int> high;
  8. vector<int> result;
  9. if (n > 1)
  10. {
  11. vector<int> low = mergesort(vector<int>(input.begin(), input.begin() + n / 2));
  12. vector<int> high = mergesort(vector<int>(input.begin() + n / 2, input.end()));
  13.  
  14. while (low.size()!=0&&high.size()!=0)
  15. {
  16. if (low.front() > high.front())
  17. {
  18. result.push_back(high.front());
  19. high.erase(high.begin());
  20.  
  21. }
  22. else
  23. {
  24. result.push_back(low.front());
  25. low.erase(low.begin());
  26. }
  27. }
  28. if (low.size() == 0)
  29. {
  30. result.insert(
  31. result.end(),
  32. std::make_move_iterator(high.begin()),
  33. std::make_move_iterator(high.end())
  34. );
  35.  
  36. }
  37. else
  38. {
  39. result.insert(
  40. result.end(),
  41. std::make_move_iterator(low.begin()),
  42. std::make_move_iterator(low.end())
  43. );
  44.  
  45. }
  46. }
  47. else
  48. {
  49.  
  50. result.push_back(input[0]);
  51. }return result;
  52.  
  53.  
  54.  
  55.  
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement