#C++, 79 76 64 bytes
C++, 79 76 64 bytes
[](auto a,auto b){while(a<--b)*a%2?*++a:(*a^=*b,*b^=*a,*a^=*b);}
This function accepts a pair of iterators (which must be random access iterators), and steadily moves them towards each other. When a
points to an odd number, it is advanced. Otherwise, a
points to an even number; b
is decremented, and iter_swap
'ed with a
. (We use XOR swap, which saves us having to include <algorithm>
- or <utility>
for std::swap
).
There are unnecessary swaps when b
points to an even number, but we're golfing, not squeezing efficiency!
##Demo auto f=[](auto a,auto b){while(a<--b)a%2?++a:(*a^=*b,*b^=*a,*a^=*b);};
Demo
auto f=[](auto a,auto b){while(a<--b)*a%2?*++a:(*a^=*b,*b^=*a,*a^=*b);};
#include <array>
#include <iostream>
int main()
{
auto a = std::array{ 3,2,2,5,2,1,2 };
f(a.begin(),a.end());
for (auto i: a)
std::cout << i << " ";
std::cout << std::endl;
}
Non-competitive answer
##Non-competitive answer
TheThe natural C++ method is std::partition
, but that comes out at 83 bytes:
#include<algorithm>
[](auto a,auto b){std::partition(a,b,[](auto x){return x&1;});}