timeractions.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <vector>
  2. #include "timer.hpp"
  3. #include "timeractions.hpp"
  4. #include <algorithm>
  5. #include <ncurses.h>
  6. #include <vector>
  7. SwapAction::SwapAction(std::vector<int> &input_vector): input_vector(input_vector){}
  8. void SwapAction::update(const Timer &input_timer){
  9. if (!is_complete) {
  10. int temp = input_vector[it_i];
  11. input_vector[it_i] = input_vector[input_vector.size() -1 - it_i];
  12. input_vector[input_vector.size() - 1 - it_i] = temp;
  13. it_i++;
  14. if(it_i>=input_vector.size() / 2){is_complete=true;}
  15. }
  16. }
  17. SortAction::SortAction(std::vector<int> &input_vector): input_vector(input_vector) {}
  18. //sorts the included array using bsort and external iterators rather than for loops
  19. void SortAction::update(const Timer &input_timer) {
  20. if (!is_complete) {
  21. /**
  22. we take a number, it_i loop
  23. compare it with its neighbour, it_j loop
  24. if it's larger swap it
  25. keep going until it_i == length - sorted numbers
  26. once that is true, reset it_j and advance it_i
  27. **/
  28. if (input_vector[it_j]>input_vector[it_j+1]) {
  29. // int temp = input_vector[it_j + 1];
  30. // input_vector[it_j + 1] = input_vector[it_j];
  31. // input_vector[it_j] = temp;
  32. std::swap(input_vector[it_j], input_vector[it_j+1]);
  33. }
  34. it_j++;
  35. if (it_j >= input_vector.size() - 1 - it_i) {
  36. it_j = 0;
  37. it_i++;
  38. }
  39. if (it_i>=input_vector.size()) {
  40. is_complete=true;
  41. }
  42. }
  43. }