patterncMinor
Constructing an odd-even mergesort sorting network
Viewed 0 times
sortingmergesortconstructingevennetworkodd
Problem
I was searching for a non-recursive odd-even mergesort implementation and found this one:
The resulting sorting network is not an odd-even-merge sort network. When one draws a diagram of the pairs, it can be seen that too many pairs are generated and others are missed.
So here is an image of a correct network with 32 inputs. A vertical line between 2 horizontal lines means compare value a[x] with a[y], if greater then swap the values in the array.
(clickable)
The code from the book translated into C:
See my StackOverflow question for more details and a diagram for the falsly generated network.
So I took the sorting network images for `length = 1
- Sedgewick R.
The resulting sorting network is not an odd-even-merge sort network. When one draws a diagram of the pairs, it can be seen that too many pairs are generated and others are missed.
So here is an image of a correct network with 32 inputs. A vertical line between 2 horizontal lines means compare value a[x] with a[y], if greater then swap the values in the array.
(clickable)
The code from the book translated into C:
#include
#include
void sort(int l, int r)
{ int n = r-l+1;
for (int p=1; p0; k/=2)
for (int j=k%p; j+k<n; j+=(k+k))
for (int i=0; i<n-j-k; i++)
if ((j+i)/(p+p) == (j+i+k)/(p+p))
printf("%2i cmp %2i\n", l+j+i, l+j+i+k);
}
int main(char* argv, int args)
{ const int COUNT = 8;
sort(0, COUNT-1);
}See my StackOverflow question for more details and a diagram for the falsly generated network.
So I took the sorting network images for `length = 1
- Can it be improved? E.g. just 4 loops.
- It need not be very fast, because it's a hardware generation and check routine.
Solution
In general your code is quite hard to comprehend due to the bracing style and the single letter variable names. A bit more spaces and more meaningful names would do quite a bit for readability (I've kept your 2-indent spacing) and also remove the need for most of the comments:
#include
#include
int log2ceil(int value)
{
int i;
int cmp = 1;
for (i = 0; cmp < value; i++)
{
cmp <<= 1;
}
return i;
}
void sort(int length)
{
int groups = log2ceil(length);
for (int group = 0; group < groups; group++)
{
int blocks = 1 << (groups - group - 1);
for (int block = 0; block < blocks; block++)
{
for (int stage = 0; stage <= group; stage++)
{
int distance = 1 << (group - stage);
int startPoint = (stage == 0) ? 0 : distance;
for (int j = startPoint; j + distance < (2 << group); j += 2 * distance)
{
for (int i = 0; i < distance; i++) // shift startpoints
{
int x = (block * (length / blocks)) + j + i;
int y = x + distance;
printf("%2i cmp %2i\n", x, y);
}
}
}
}
}
}
int main(char* argv, int args)
{
const int COUNT = 8;
sort(COUNT);
}Code Snippets
#include <stdlib.h>
#include <stdio.h>
int log2ceil(int value)
{
int i;
int cmp = 1;
for (i = 0; cmp < value; i++)
{
cmp <<= 1;
}
return i;
}
void sort(int length)
{
int groups = log2ceil(length);
for (int group = 0; group < groups; group++)
{
int blocks = 1 << (groups - group - 1);
for (int block = 0; block < blocks; block++)
{
for (int stage = 0; stage <= group; stage++)
{
int distance = 1 << (group - stage);
int startPoint = (stage == 0) ? 0 : distance;
for (int j = startPoint; j + distance < (2 << group); j += 2 * distance)
{
for (int i = 0; i < distance; i++) // shift startpoints
{
int x = (block * (length / blocks)) + j + i;
int y = x + distance;
printf("%2i cmp %2i\n", x, y);
}
}
}
}
}
}
int main(char* argv, int args)
{
const int COUNT = 8;
sort(COUNT);
}Context
StackExchange Code Review Q#114824, answer score: 3
Revisions (0)
No revisions yet.