timeractions.cpp 1.4 KB

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