home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
ddjmag
/
ddj8603.arc
/
WISSBAUM.MAR
< prev
Wrap
Text File
|
1986-03-31
|
3KB
|
92 lines
/* */
/* Bose-Nelson Sort - Recursive Version */
/* */
/* by R J Wissbaum */
/* 15553 E Wyoming Dr - H */
/* Aurora, CO 80012 */
/* */
/* Reference: */
/* Dr. Dobb's Journal */
/* September 1985 (#107) */
/* Page 68 */
/* */
/* */
#include "STDIO.H"
int count = 0;
main ( argc, argv )
int argc,*argv;
{ int n;
char arg[255];
if ( argc <2 )
{ printf ( "USAGE: bose5 n >outfile \n" );
exit();
}
else
{ getarg( 1, arg, 255, argc, argv );
n = atoi( arg );
if ( n<0 ) n = -n;
bosesort ( 2, 1, n, 0, 0 );
printf ( "There were %d swaps \n", count );
}
}
bosesort( n, i, x, j, y )
int n, i, x, j, y;
{ int a, b;
switch ( n )
{
case 0: { /* printf ( "}\n" ); */
}
break;
case 1: { /* printf ( "swap ( %d, %d ); \n", i, x ); */
count++;
}
break;
case 2: { if ( x != 1 )
{ a = ( x / 2 );
bosesort ( 3, i, a, (i+a), (x-a) );
bosesort ( 2, (i+a), (x-a), 0, 0 );
bosesort ( 2, i, a, 0, 0 );
}
}
break;
case 3: { a = ( x / 2 );
if ( even ( x ) ) b = ( y + 1 ) / 2;
else b = ( y / 2 );
if ( ( x == 1 ) && ( y == 1 ) ) bosesort ( 1, i, j, 0, 0 );
else if ( ( x == 1 ) && ( y == 2 ) )
{
bosesort ( 1, i, j, 0, 0 );
bosesort ( 1, i, (j+1), 0, 0 );
}
else if ( ( x == 2 ) && ( y == 1 ) )
{
bosesort ( 1, (i+1), j, 0, 0 );
bosesort ( 1, i, j, 0, 0 );
}
else if ( ( x != 0 ) && ( y != 0 ) )
{
bosesort ( 3, (i+a), (x-a), j, b );
bosesort ( 3, (i+a), (x-a), (j+b), (y-b) );
bosesort ( 3, i, a, j, b );
}
}
break;
default: {
printf ( "FATAL: Error in stack \n" );
exit ();
}
break;
} /* End of WHILE loop */
} /* End of BOSESORT */
even ( x )
int x;
{ return ( 1 - (1 & x ) ); }
bre