#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/****Copyright (c) 1997,1998,1999-2002 All rights reserved
 ****A. V. Gerbessiotis 
 ****Permission to use, copy, modify, and distribute this software,
 ****and to incorporate it, in whole or in part, into 
 ****other software,is hereby granted without fee, provided that
 ****  (1) the above copyright notice and this permission 
 ****      notice appear in all copies of the source code, and 
 ****      the above copyright notice appear in clearly visible 
 ****      form on all supporting documentation and distribution 
 ****      media, and
 ****     (2) any redistribution of the software, in original
 ****         or modified form, be without fee and subject to
 ****         these same conditions.
 ****   No guarantee, whatsoever, is offered that the code works as
 ****   advertised or is absent of any, even damaging,
 ****   side-effects. Use this code at your own personal risk.
 ****/

void quickerg(char *keys, int n, int size, int (*compare) ())
{
	register int i,j,l,r,s;
	char	*x,*y;
	int	lstack[1000];
	int	rstack[1000];

	x = (char *) malloc(size*sizeof(char));
	y = (char *) malloc(size*sizeof(char));
	s=0;
	lstack[0]=0;rstack[0]=n-1;
do {
	l=lstack[s]; r=rstack[s]; s--;
	do {
		i=l;
		j=r;
		memcpy(x,&keys[((l+r)/2)*size],size);

		do {
			while (compare(&keys[i*size],x) < 0) i++;
			while (compare(x,&keys[j*size]) < 0) j--;

			if (i <= j )
			{
				memcpy(y,&keys[i*size],size);
				memcpy(&keys[i*size],&keys[j*size],size);
				memcpy(&keys[j*size],y,size);
				i++;
				j--;
			}
		} while (i <= j); 
		
		if (i < r)
		{
			s++; lstack[s]=i; rstack[s]=r;
		}
		r=j;


	} while ( l < r);

} while (s>= 0);

}
