Category : Utilities for DOS and Windows Machines
Archive   : COPPY36.ZIP
Filename : SORT.C

 
Output of file : SORT.C contained in archive : COPPY36.ZIP
/* QSORT.C
* =======
* Quit sort routine from Dr. Dobb's Journal #102 April 1985.
*/

#define STAND 0
#include

#if STAND
#define MAX_PTRS 500
static char *pp[ MAX_PTRS ];
static int Lev = 0; /* Recusion level */
static int Maxlev = 0; /* Maximum */
#endif

typedef int (* PFI)(); /* Pointer to a function that */
/* returns an int. */
static PFI Comp; /* Pointer to comparsion routine */
static int Width; /* Width of an object in bytes */
/* ------------------------------------------------------------------ */
sort(base,cnt)
char *base;
int cnt;
{
qsort(base,cnt,sizeof(PFI),0);
return(0);
}

/* ------------------------------------------------------------------ */
int argvcmp( slp, s2p )
char **slp,
**s2p;
/* Comparsion routine for sorting an argv like list of pointers
* to strings. Just remove one level of indirection and call strcmp
* to do the comparsion.
*/
{
return( strcmp( *slp, *s2p ) * -1);
}
qsort( base, nel, width, compare )
char *base;
int nel,
width,
(*compare)();
/* Perform a quick sort on an array starting at base.
*/
{

Width = width;
Comp = ( compare == (PFI)0 ) ? &argvcmp : compare ;

if ( nel > 1 )
rqsort( base, base + ( ( nel - 1 ) * width ) );

#if STAND
ptext( nel, base );
#endif

}

/* ------------------------------------------------------------------ */

static rqsort( low, high )
register char *low,
*high;
/* Workhorse function called by the access routine, qsort().
*/
{ char *pivot,
*base;
static char *a, /* Used for exchange, will not be needed */
*b; /* during recusion, so they can be static.*/
static int tmp, /* That way they will not take up stack */
i; /* space. */

base = low ; /* Remember base address of array. */
pivot = high ; /* Partition off the pivot. */
high -= Width;

do
{ while ( low < high && (*Comp)(low, pivot) <= 0 )
low += Width;
while ( low < high && (*Comp)(high, pivot) >= 0 )
high -= Width;

if ( low < high ) /* Exchange low & high */
{
/*
printf( "lev = %d: exchangeing high: <%s> & low <%s>\n",
Lev, *((char **)high), *((char **)low));
*/
for ( b = low, a = high, i = Width; --i >= 0; a++, b++ )
{ tmp = *b; /* Exchange *low and *high */
*b = *a;
*a = tmp;
}
}
} while ( low < high );

if ( low < pivot && (*Comp)(low, pivot) > 0 )
for ( b = low, a = pivot, i = Width; --i >= 0; a++, b++ )
{ tmp = *b; /* Exchange *low and *pivot */
*b = *a;
*a = tmp;
}

low += Width;

if ( high - base < pivot - low )
{
if ( low < pivot )
rqsort( low, pivot );
if ( base < high )
rqsort( base, high );
}
else
{
if ( base < high )
rqsort( base, high );
if ( low < pivot )
rqsort( low, pivot );
}
}

/* ------------------------------------------------------------------ */

#if STAND
static ptext( argc, argv )
int argc;
char **argv;
/* Print out argv, one element per line */
{ register int i;

for ( i = 1; --argc >= 0; i++ )
printf( "%s\n", *argv++ );
}

main( argc, argv )
int argc;
char **argv;
{ char *malloc(),
*wk;

if ( argc > 1 )
qsort( ++argv, --argc, sizeof(PFI), 0 );
else
{ argc = 0;
while ( 1 )
{ if ( ( wk = malloc( 80 ) ) == 0 )
{ printf( "\nUnable to Malloc\n" );
exit( 0 );
}
if ( gets( wk ) == NULL )
break;
pp[ argc++ ] = wk ;
if ( argc > MAX_PTRS )
{ printf( "\nToo many items for QSORT\n" );
exit( 0 );
}
}
printf("\r\n" );
qsort( pp, argc, sizeof(PFI), 0 );
}
}
#endif


  3 Responses to “Category : Utilities for DOS and Windows Machines
Archive   : COPPY36.ZIP
Filename : SORT.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/