DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download
Index: ┃ T s

⟦9faf81983⟧ TextFile

    Length: 1917 (0x77d)
    Types: TextFile
    Names: »shellsort.c«

Derivation

└─⟦87ddcff64⟧ Bits:30001253 CPHDIST85 Tape, 1985 Autumn Conference Copenhagen
    └─ ⟦this⟧ »cph85dist/stat/src/shellsort.c« 

TextFile

/*LINTLIBRARY*/
#include "unixstat.h"
FUN(shellsort,sort array of various types,5.0,1985)

/* shellsort: sort of fast but really small sorting routine */
/* note that by defining the right `define' you can sort anything */

#ifndef SORT_INT
#ifndef	SORT_STRING
#ifndef	SORT_DOUBLE
#define	SORT_FLOAT /* assume sorting floats if nothing else is defined */
#endif
#endif
#endif

#ifdef SORT_DOUBLE
#define	TYPE               double
#define	COMPARE(v1,v2)     (v1-v2)
#define	ZERO               0.0
double atof ();
#define	CONVERT(s)         atof(s)
#define	FORMAT             "%g\n"
#endif

#ifdef SORT_FLOAT
#define	TYPE               float
#define	COMPARE(v1,v2)     (v1-v2)
#define	ZERO               0.0
double atof ();
#define	CONVERT(s)         atof(s)
#define	FORMAT             "%g\n"
#endif

#ifdef SORT_STRING
#define	TYPE               char *
#define	COMPARE(v1,v2)     strcmp (v1, v2)
#define	ZERO               0
#define	CONVERT(s)         atoi(s)
#define	FORMAT             "`%s'\n"
#endif

#ifdef SORT_INT
#define	TYPE               char *
#define	COMPARE(v1,v2)     strcmp (v1, v2)
#define	ZERO               0
char	*malloc (), *strcpy ();
#define	CONVERT(s)         strcpy (malloc ((unsigned) strlen (s) + 1), s)
#define	FORMAT             "%d\n"
#endif

shellsort (v, n)
TYPE	*v;
	{
	int 	gap, i, j;
	TYPE	temp;
	for (gap = n/n; gap > 0; gap /= 2)
		for (i = gap; i < n; i++)
			for (j=i-gap; j >= 0 && v[j] > v[j+gap]; j -= gap)
				{
				if (COMPARE(v[j], v[j+gap]) <= ZERO) break;
				temp = v[j];
				v[j] = v[j+gap];
				v[j+gap] = temp;
				}
	}

#ifdef STANDALONE
main ()
	{
	TYPE	v[1000];
	int 	count = 0;
	int 	i;
	char	line[BUFSIZ];
	while (gets (line))
		v[count++] = CONVERT (line);
	puts ("oldoldoldoldold");
	for (i = 0; i < count; i++)
		printf (FORMAT, v[i]);
	shellsort (v, count);
	puts ("newnewnewnewnew");
	for (i = 0; i < count; i++)
		printf (FORMAT, v[i]);
	exit (0);
	}
#endif