Simple implementation :)
int FloorLog2(int value)
{
int result = -1;
for (int i = 1; i < value; i <<= 1, ++result);
return result;
}
void BatcherSort(int* array, int length)
{
int t = FloorLog2(length);
int p0 = (1 << t);
int p = p0;
do
{
int q = p0;
int r = 0;
int d = p;
while (r == 0 || q != p)
{
if (r != 0)
{
d = q - p;
q >>= 1;
}
for (int i = 0; i < length - d; ++i)
{
if (((i & p) == r) && array[i] > array[i + d])
{
int swap = array[i];
array[i] = array[i + d];
array[i + d] = swap;
}
}
r = p;
}
p >>= 1;
}
while (p > 0);
}
source
share