magnum opus
This commit is contained in:
parent
79416aa255
commit
33e60cd710
1 changed files with 4 additions and 7 deletions
7
main.cpp
7
main.cpp
|
@ -21,8 +21,6 @@ std::vector<std::size_t> getReversalsToSort(const std::vector<int> &arr) {
|
||||||
std::vector<int> intermediate = arr;
|
std::vector<int> intermediate = arr;
|
||||||
std::vector<std::size_t> ret = {};
|
std::vector<std::size_t> ret = {};
|
||||||
|
|
||||||
// a bit more explicit than necessary but my brain thought it was useful info
|
|
||||||
;
|
|
||||||
for (std::size_t i = intermediate.size(); i > 1; i--) {
|
for (std::size_t i = intermediate.size(); i > 1; i--) {
|
||||||
|
|
||||||
// only searching the unsorted elements
|
// only searching the unsorted elements
|
||||||
|
@ -33,17 +31,16 @@ std::vector<std::size_t> getReversalsToSort(const std::vector<int> &arr) {
|
||||||
std::size_t second_swap = i;
|
std::size_t second_swap = i;
|
||||||
// remove false positives of elements already being in the correct spot
|
// remove false positives of elements already being in the correct spot
|
||||||
if (first_swap != second_swap) {
|
if (first_swap != second_swap) {
|
||||||
// also remove reverses where nothing changes
|
// if the element is already the first in the list we don't need to move
|
||||||
|
// it there
|
||||||
if (first_swap > 1) {
|
if (first_swap > 1) {
|
||||||
ret.push_back(first_swap);
|
ret.push_back(first_swap);
|
||||||
std::reverse(intermediate.begin(), intermediate.begin() + first_swap);
|
std::reverse(intermediate.begin(), intermediate.begin() + first_swap);
|
||||||
}
|
}
|
||||||
if (second_swap > 1) {
|
|
||||||
ret.push_back(second_swap);
|
ret.push_back(second_swap);
|
||||||
std::reverse(intermediate.begin(), intermediate.begin() + second_swap);
|
std::reverse(intermediate.begin(), intermediate.begin() + second_swap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue