Skip to main content
4 of 7
-3 bytes; thanks @PeterCordes
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41

#C++, 79 76 bytes

#include<utility>
[](auto a,auto b){while(a<--b)std::swap(*b,*a%2?*++a:*a);}

That's 18 bytes for the include line, and 58 for the function itself.

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 std::swap, as the extra * are a net benefit compared to the longer name and include - <algorithm> versus <utility>).

There are unnecessary swaps when b points to an even number, but we're golfing, not squeezing efficiency!

##Demo #include

auto f=[](auto a,auto b){while(a<--b)std::swap(*b,*a%2?*++a:*a);};

#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;
}
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41