Odds before Evens Given a list of integers, rearrange them so that all of the odd integers appear before all of the even integers, without otherwise changing the order.
An obvious solution is
OddBeforeEven[data_List]:= Join[Cases[data, _?OddQ], Cases[data, _?EvenQ]]
But this isn't particularly efficient, so maybe we can try
OddBeforeEven[data_List]:= Join[Select[data, OddQ], Select[data, EvenQ]]
Now, an innovative solution could also be something like
OddBeforeEven[data_List]:= Flatten@GatherBy[data, OddQ]
But I can't seem to improve it further.
Can we optimize OddQ and EvenQ using Bit operations?
With[{split = GroupBy[x, OddQ]}, Join[split[True], split[False]]]
. It's quite annoying that the GatherBy solution doesnt work (because Wolfram sorts the association according to the number of elements in each Key)— it's very efficient and does things in a single pass. $\endgroup$