#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define	ALN2I	1.442695022
#define	TINY	1.0e-5

extern double log();
extern double floor();

/* May be based on a N. Wirth PASCAL code or Numerical Recipes code */

void shellng(char *keys, int n, int size, int (*compare) ())
{
	register int nn,m,i,j,lognb2;
	double t;
	char *x;

	x = (char *) malloc(size*sizeof(char));
	lognb2 = (log((double)n)*ALN2I+TINY);
	m=n;
	for(nn=1;nn<=lognb2;nn++){
		m >>=1;
		for(j=m;j<n;j++){
			i=j-m;
			memcpy(x,&keys[j*size],size);
			while (i>=0 && compare(&keys[i*size],x)>0) {
				memcpy(&keys[(i+m)*size],&keys[i*size],size);
				i-=m;
				}
			memcpy(&keys[(i+m)*size],x,size);
			
		}
	}

}
