50 lines
1.3 KiB
C++
50 lines
1.3 KiB
C++
#include <vector>
|
|
#include "timer.hpp"
|
|
#include "timeractions.hpp"
|
|
#include <algorithm>
|
|
#include <ncurses.h>
|
|
#include <vector>
|
|
|
|
SwapAction::SwapAction(std::vector<int> &input_vector): input_vector(input_vector){}
|
|
|
|
void SwapAction::update(const Timer &input_timer){
|
|
if (!is_complete) {
|
|
int temp = input_vector[it_i];
|
|
input_vector[it_i] = input_vector[input_vector.size() -1 - it_i];
|
|
input_vector[input_vector.size() - 1 - it_i] = temp;
|
|
it_i++;
|
|
if(it_i>=input_vector.size() / 2){is_complete=true;}
|
|
}
|
|
}
|
|
|
|
|
|
SortAction::SortAction(std::vector<int> &input_vector): input_vector(input_vector) {}
|
|
|
|
//sorts the included array using bsort and external iterators rather than for loops
|
|
void SortAction::update(const Timer &input_timer) {
|
|
if (!is_complete) {
|
|
/**
|
|
we take a number, it_i loop
|
|
compare it with its neighbour, it_j loop
|
|
if it's larger swap it
|
|
keep going until it_i == length - sorted numbers
|
|
once that is true, reset it_j and advance it_i
|
|
**/
|
|
if (input_vector[it_j]>input_vector[it_j+1]) {
|
|
// int temp = input_vector[it_j + 1];
|
|
// input_vector[it_j + 1] = input_vector[it_j];
|
|
// input_vector[it_j] = temp;
|
|
std::swap(input_vector[it_j], input_vector[it_j+1]);
|
|
}
|
|
it_j++;
|
|
|
|
if (it_j >= input_vector.size() - 1 - it_i) {
|
|
it_j = 0;
|
|
it_i++;
|
|
}
|
|
if (it_i>=input_vector.size()) {
|
|
is_complete=true;
|
|
}
|
|
}
|
|
}
|