Created
June 9, 2017 18:12
-
-
Save FirefoxMetzger/124fc69f551bfb950aacf8a47d41110b to your computer and use it in GitHub Desktop.
Source to answer question at: https://www.reddit.com/r/TheHappieMakers/comments/65oay5/challenge_rearrange_an_array/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <algorithm> | |
#include <iostream> | |
#include <iterator> | |
#include <vector> | |
template <typename I> | |
void sub_divide_rotate(I first, I last) { | |
auto mid = std::distance(first, last) / 2; | |
if (mid <= 1) return; | |
std::rotate(std::next(first), std::next(first, mid), std::prev(last)); | |
sub_divide_rotate(std::next(first), std::prev(last)); | |
} | |
int main() { | |
std::vector<int> v = {1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10, 12}; | |
sub_divide_rotate(std::begin(v), std::end(v)); | |
assert(std::is_sorted(std::begin(v), std::end(v))); | |
for (int i : v) { | |
std::cout<<i<<' '; | |
} | |
std::cout << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment