Skip to content

Instantly share code, notes, and snippets.

@FirefoxMetzger
Created June 9, 2017 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FirefoxMetzger/124fc69f551bfb950aacf8a47d41110b to your computer and use it in GitHub Desktop.
Save FirefoxMetzger/124fc69f551bfb950aacf8a47d41110b to your computer and use it in GitHub Desktop.
#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