Bose-Nelson Sort Network Algorithm - GitHub - atinm
Bose-Nelson Sort Network Algorithm - GitHub - atinm
P(int i, int j)
{
/* print out in 0 based notation */
printf("swap(%d, %d);\n", i-1, j-1);
}
if(x == 1 && y == 1)
P(i, j); /* 1 comparison sorts 2 items */
else if(x == 1 && y == 2)
{
/* 2 comparisons inserts an item into an
* already sorted sequence of length 2. */
P(i, (j + 1));
P(i, j);
}
else if(x == 2 && y == 1)
{
/* As above, but inserting j */
P(i, j);
P((i + 1), j);
}
else
{
/* Recurse on shorter sequences, attempting
* to make the length of one subsequence odd
* and the length of the other even. If we
* can do this, we eventually merge the two. */
a = x/2;
b = (x & 1) ? (y/2) : ((y + 1)/2);
Pbracket(i, a, j, b);
Pbracket((i + a), (x - a), (j + b), (y - b));
Pbracket((i + a), (x - a), j, b);
}
}
if(m > 1)
{
/* Partition into 2 shorter sequences,
* generate a sorting method for each,
* and merge the two sub-networks. */
a = m/2;
Pstar(i, a);
Pstar((i + a), (m - a));
Pbracket(i, a, (i + a), (m - a));
}
}
void bose(int n)
{
Pstar(1, n); /* sort the sequence {X1,...,Xn} */
}
https://raw.githubusercontent.com/atinm/bose-nelson/refs/heads/master/bose-nelson.c 1/2
04/11/2024, 16:50 raw.githubusercontent.com/atinm/bose-nelson/refs/heads/master/bose-nelson.c
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("Usage:\n");
printf("\t bose-nelson <n>\n");
printf("\t\twhere n is the number of items to sort\n");
return(-1);
}
int n = atoi(argv[1]);
bose(n);
return 0;
}
/* End of File */
https://raw.githubusercontent.com/atinm/bose-nelson/refs/heads/master/bose-nelson.c 2/2