using max_element instead of find and sorted list
This commit is contained in:
parent
76bd3dbbfa
commit
79416aa255
1 changed files with 3 additions and 10 deletions
13
main.cpp
13
main.cpp
|
@ -18,22 +18,16 @@ std::vector<std::size_t> getReversalsToSort(const std::vector<int> &arr);
|
|||
*/
|
||||
std::vector<std::size_t> getReversalsToSort(const std::vector<int> &arr) {
|
||||
|
||||
// there's a slightly more fancy version of this but who cares
|
||||
// all we need is the elements sorted from least to greatest
|
||||
// sorting to get info might be cheating but eeeeeh it wasn't specified
|
||||
std::vector<int> sorted = arr;
|
||||
std::sort(sorted.begin(), sorted.end());
|
||||
|
||||
std::vector<int> intermediate = arr;
|
||||
std::vector<std::size_t> ret = {};
|
||||
|
||||
// a bit more explicit than necessary but my brain thought it was useful info
|
||||
std::size_t i = intermediate.size();
|
||||
for (auto elem = sorted.rbegin(); elem != sorted.rend(); elem++) {
|
||||
;
|
||||
for (std::size_t i = intermediate.size(); i > 1; i--) {
|
||||
|
||||
// only searching the unsorted elements
|
||||
auto found =
|
||||
std::find(intermediate.cbegin(), intermediate.cbegin() + i, *elem);
|
||||
std::max_element(intermediate.cbegin(), intermediate.cbegin() + i);
|
||||
|
||||
std::size_t first_swap = found - intermediate.cbegin() + 1;
|
||||
std::size_t second_swap = i;
|
||||
|
@ -49,7 +43,6 @@ std::vector<std::size_t> getReversalsToSort(const std::vector<int> &arr) {
|
|||
std::reverse(intermediate.begin(), intermediate.begin() + second_swap);
|
||||
}
|
||||
}
|
||||
i--;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
Loading…
Reference in a new issue