49 lines
696 B
C
Executable File
49 lines
696 B
C
Executable File
#ident "@(#)gsort.c 1.6 93/06/08 SMI" /* From AT&T Toolchest */
|
|
|
|
/*
|
|
* gsort - sort an array of pointers to data
|
|
*
|
|
* David Korn
|
|
* AT&T Bell Laboratories
|
|
* Room 3C-526B
|
|
* Murray Hill, N. J. 07974
|
|
* Tel. x7975
|
|
*
|
|
* Derived from Bourne Shell
|
|
*/
|
|
|
|
/*
|
|
* sort the array of strings argv with n elements
|
|
*/
|
|
|
|
void gsort(argv,n, fn)
|
|
char *argv[];
|
|
int (*fn)();
|
|
{
|
|
register int i, j, m;
|
|
int k;
|
|
for(j=1; j<=n; j*=2);
|
|
for(m=2*j-1; m/=2;)
|
|
{
|
|
k=n-m;
|
|
for(j=0; j<k; j++)
|
|
{
|
|
for(i=j; i>=0; i-=m)
|
|
{
|
|
register char **ap;
|
|
ap = &argv[i];
|
|
if((*fn)(ap[m],ap[0])>0)
|
|
break;
|
|
else
|
|
{
|
|
char *s;
|
|
s=ap[m];
|
|
ap[m]=ap[0];
|
|
ap[0]=s;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|