Category : Recently Uploaded Files
Archive   : SNIP9503.ZIP
Filename : HUGESORT.C

 
Output of file : HUGESORT.C contained in archive : SNIP9503.ZIP
/*
** hugesort.c -- huge qsort() -- public domain by Raymond Gardner 6/92
** tested with Borland C++ 3.0 (C mode)
*/

static void swap(char huge *a, char huge *b, unsigned n)
{
char tmp;

do
{
tmp = *a; *a++ = *b; *b++ = tmp;
} while ( --n );
}
void hugesort(void huge *basep,
unsigned nel,
unsigned width,
int (*comp)(void huge *, void huge *))
{
char huge *i, huge *j;
unsigned int lnel, rnel;
char huge *base = (char huge *)basep;

while (nel > 1)
{
swap(base, base + (long)width * (nel / 2), width);
for (i = base, j = base + (long)width * nel; ; )
{
do
j -= width;
while ( (*comp)(j, base) > 0 );
do
i += width;
while ( i < j && (*comp)(i, base) < 0 );
if (i >= j)
break;
swap(i, j, width);
}
swap(j, base, width);
lnel = (unsigned)((long)(j - base) / width);
rnel = nel - lnel - 1;
j += width;
if (lnel < rnel)
{
hugesort(base, lnel, width, comp);
base = j;
nel = rnel;
}
else
{
hugesort(j, rnel, width, comp);
nel = lnel;
}
}
}

#ifdef TEST

#include
#include
#include
#include

#define PADSIZE 300

typedef struct x {
int key;
char pad[PADSIZE];
} X;

int cmpv(void huge *a, void huge *b) /* (note void huge *) passed here */
{
return ((X huge *)a)->key < ((X huge *)b)->key ? -1 :
((X huge *)a)->key > ((X huge *)b)->key ? 1 : 0;
}

int main(int argc, char **argv)
{
X huge *v;
int n;
int i, j;

n = 300; /* default element count */
if (argc > 1)
n = atoi(argv[1]);
printf("test with %d elements\n", n);
v = farmalloc(sizeof(X) * (long)n);
assert(v); /* be sure we got memory */
for (i = 0; i < n; ++i) /* random init */
{
v[i].key = rand();
for (j = 0; j < PADSIZE; ++j)
v[i].pad[j] = rand();
}
for (i = 0; i < n; ++i) /* display before */
printf(" %d", v[i].key);
printf("\n");
hugesort(v, n, sizeof(X), cmpv); /* sort it */
for ( i = 0; i < n; ++i ) /* display after */
printf(" %d", v[i].key);
printf("\n");
return 0;
}

#endif


  3 Responses to “Category : Recently Uploaded Files
Archive   : SNIP9503.ZIP
Filename : HUGESORT.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/