home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_11_07
/
shell.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-12-12
|
999b
|
35 lines
#include <stdlib.h>
#include <malloc.h>
int my_shellsort( char *data, unsigned n_elements, unsigned esize,
int (*compare)(void *elem1, void *elem2))
{
unsigned i, gap, spacing;
char *tptr, *endptr, *tptr1, *tbuff;
int rval = 0;
tbuff = _alloca( esize);
for( gap = 1; gap < n_elements / 3U; gap = gap * 3 + 1);
;
endptr = data + n_elements * esize;
while( gap)
{
spacing = esize * gap;
for( i = 0; i < gap; i++)
for( tptr = data + i * esize; tptr < endptr; tptr = tptr1)
if( (compare)(tptr, tptr1 = tptr + spacing) > 0)
{
char *back_up;
memcpy( tbuff, tptr, esize);
memcpy( tptr, tptr1, esize);
memcpy( tptr1, tbuff, esize);
if( (back_up = tptr - spacing) >= data)
tptr1 = back_up;
rval = 1;
}
gap /= 3;
}
return( rval);
}