xref: /csrg-svn/lib/libc/stdlib/heapsort.c (revision 51206)
149989Sbostic /*-
249989Sbostic  * Copyright (c) 1991 The Regents of the University of California.
349989Sbostic  * All rights reserved.
449989Sbostic  *
550466Sbostic  * This code is derived from software contributed to Berkeley by
650466Sbostic  * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias.
750466Sbostic  *
849989Sbostic  * %sccs.include.redist.c%
949989Sbostic  */
1049989Sbostic 
1149989Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*51206Selan static char sccsid[] = "@(#)heapsort.c	5.6 (Berkeley) 09/27/91";
1349989Sbostic #endif /* LIBC_SCCS and not lint */
1449989Sbostic 
1549989Sbostic #include <sys/types.h>
1649989Sbostic #include <errno.h>
1749989Sbostic #include <stdlib.h>
1851173Sbostic #include <stddef.h>
1949989Sbostic 
2049989Sbostic /*
2149989Sbostic  * Swap two areas of size number of bytes.  Although qsort(3) permits random
2249989Sbostic  * blocks of memory to be sorted, sorting pointers is almost certainly the
2349989Sbostic  * common case (and, were it not, could easily be made so).  Regardless, it
2449989Sbostic  * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer
2549989Sbostic  * arithmetic gets lost in the time required for comparison function calls.
2649989Sbostic  */
27*51206Selan #define	SWAP(a, b, count, size, tmp) { \
28*51206Selan 	count = size; \
2949989Sbostic 	do { \
30*51206Selan 		tmp = *a; \
3149989Sbostic 		*a++ = *b; \
32*51206Selan 		*b++ = tmp; \
33*51206Selan 	} while (--count); \
3449989Sbostic }
3549989Sbostic 
3651173Sbostic /* Copy one block of size size to another. */
37*51206Selan #define COPY(a, b, count, size, tmp1, tmp2) { \
38*51206Selan 	count = size; \
39*51206Selan 	tmp1 = a; \
40*51206Selan 	tmp2 = b; \
4150789Selan 	do { \
42*51206Selan 		*tmp1++ = *tmp2++; \
43*51206Selan 	} while (--count); \
4450789Selan }
4550789Selan 
4650789Selan /*
4749989Sbostic  * Build the list into a heap, where a heap is defined such that for
4849989Sbostic  * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N.
4949989Sbostic  *
5049989Sbostic  * There two cases.  If j == nmemb, select largest of Ki and Kj.  If
5149989Sbostic  * j < nmemb, select largest of Ki, Kj and Kj+1.
5249989Sbostic  */
53*51206Selan #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \
54*51206Selan 	for (par_i = initval; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \
55*51206Selan 		child = (char *)bot + child_i * size; \
56*51206Selan 		if (child_i < nmemb && compar(child, child + size) < 0) { \
57*51206Selan 			child += size; \
58*51206Selan 			++child_i; \
5949989Sbostic 		} \
60*51206Selan 		par = (char *)bot + par_i * size; \
61*51206Selan 		if (compar(child, par) <= 0) \
6249989Sbostic 			break; \
63*51206Selan 		SWAP(par, child, count, size, tmp); \
6449989Sbostic 	} \
6549989Sbostic }
6649989Sbostic 
6749989Sbostic /*
6850789Selan  * Select the top of the heap and 'heapify'.  Since by far the most expensive
6951173Sbostic  * action is the call to the compar function, a considerable optimization
7050789Selan  * in the average case can be achieved due to the fact that k, the displaced
7150789Selan  * elememt, is ususally quite small, so it would be preferable to first
7250789Selan  * heapify, always maintaining the invariant that the larger child is copied
7350789Selan  * over its parent's record.
7450789Selan  *
7551173Sbostic  * Then, starting from the *bottom* of the heap, finding k's correct place,
7651173Sbostic  * again maintianing the invariant.  As a result of the invariant no element
7751173Sbostic  * is 'lost' when k is assigned its correct place in the heap.
7850789Selan  *
7950789Selan  * The time savings from this optimization are on the order of 15-20% for the
8050789Selan  * average case. See Knuth, Vol. 3, page 158, problem 18.
8150789Selan  */
82*51206Selan #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \
83*51206Selan 	for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \
84*51206Selan 		child = (char *)bot + child_i * size; \
85*51206Selan 		if (child_i < nmemb && compar(child, child + size) < 0) { \
86*51206Selan 			child += size; \
87*51206Selan 			++child_i; \
8850789Selan 		} \
89*51206Selan 		par = (char *)bot + par_i * size; \
90*51206Selan 		COPY(par, child, count, size, tmp1, tmp2); \
9150789Selan 	} \
9251173Sbostic 	for (;;) { \
93*51206Selan 		child_i = par_i; \
94*51206Selan 		par_i = child_i / 2; \
95*51206Selan 		child = (char *)bot + child_i * size; \
96*51206Selan 		par = (char *)bot + par_i * size; \
97*51206Selan 		if (child_i == 1 || compar(k, par) < 0) { \
98*51206Selan 			COPY(child, k, count, size, tmp1, tmp2); \
9950789Selan 			break; \
10050789Selan 		} \
101*51206Selan 		COPY(child, par, count, size, tmp1, tmp2); \
10250789Selan 	} \
10350789Selan }
10450789Selan 
10550789Selan /*
10649989Sbostic  * Heapsort -- Knuth, Vol. 3, page 145.  Runs in O (N lg N), both average
10749989Sbostic  * and worst.  While heapsort is faster than the worst case of quicksort,
10849989Sbostic  * the BSD quicksort does median selection so that the chance of finding
10949989Sbostic  * a data set that will trigger the worst case is nonexistent.  Heapsort's
11051173Sbostic  * only advantage over quicksort is that it requires little additional memory.
11149989Sbostic  */
11250789Selan int
11349989Sbostic heapsort(bot, nmemb, size, compar)
11451173Sbostic 	void *bot;
11551173Sbostic 	size_t nmemb, size;
11651173Sbostic 	int (*compar) __P((const void *, const void *));
11749989Sbostic {
11851173Sbostic 	register int cnt, i, j, l;
119*51206Selan 	register char tmp, *tmp1, *tmp2;
12051173Sbostic 	char *k, *p, *t;
12149989Sbostic 
12249989Sbostic 	if (nmemb <= 1)
12349989Sbostic 		return (0);
12451173Sbostic 
12549989Sbostic 	if (!size) {
12649989Sbostic 		errno = EINVAL;
12749989Sbostic 		return (-1);
12849989Sbostic 	}
12951173Sbostic 
13051173Sbostic 	if ((k = malloc(size)) == NULL)
13151173Sbostic 		return (-1);
13251173Sbostic 
13349989Sbostic 	/*
13449989Sbostic 	 * Items are numbered from 1 to nmemb, so offset from size bytes
13549989Sbostic 	 * below the starting address.
13649989Sbostic 	 */
13749989Sbostic 	bot -= size;
13849989Sbostic 
13949989Sbostic 	for (l = nmemb / 2 + 1; --l;)
140*51206Selan 		CREATE(l, nmemb, i, j, t, p, size, cnt, tmp);
14149989Sbostic 
14249989Sbostic 	/*
14349989Sbostic 	 * For each element of the heap, save the largest element into its
14450789Selan 	 * final slot, save the displaced element (k), then recreate the
14550789Selan 	 * heap.
14649989Sbostic 	 */
14749989Sbostic 	while (nmemb > 1) {
148*51206Selan 		COPY(k, (char *)bot + nmemb * size, cnt, size, tmp1, tmp2);
149*51206Selan 		COPY((char *)bot + nmemb * size, (char *)bot + size,  cnt, size, tmp1, tmp2);
15049989Sbostic 		--nmemb;
151*51206Selan 		SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2);
15249989Sbostic 	}
15351173Sbostic 	free(k);
15449989Sbostic 	return (0);
15549989Sbostic }
156