xref: /csrg-svn/lib/libc/stdlib/heapsort.c (revision 51173)
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)
1250789Selan static char sccsid[] = "@(#)heapsort.c	1.3 (Berkeley) 7/29/91";
1349989Sbostic #endif /* LIBC_SCCS and not lint */
1449989Sbostic 
1549989Sbostic #include <sys/types.h>
1649989Sbostic #include <errno.h>
1749989Sbostic #include <stdlib.h>
18*51173Sbostic #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  */
2749989Sbostic #define	SWAP(a, b) { \
28*51173Sbostic 	cnt = size; \
2949989Sbostic 	do { \
3049989Sbostic 		ch = *a; \
3149989Sbostic 		*a++ = *b; \
3249989Sbostic 		*b++ = ch; \
3349989Sbostic 	} while (--cnt); \
3449989Sbostic }
3549989Sbostic 
36*51173Sbostic /* Copy one block of size size to another. */
37*51173Sbostic #define COPY(a, b) { \
38*51173Sbostic 	cnt = size; \
39*51173Sbostic 	t1 = a; \
40*51173Sbostic 	t2 = b; \
4150789Selan 	do { \
4250789Selan 		*t1++ = *t2++; \
4350789Selan 	} while (--cnt); \
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  */
5350789Selan #define CREATE(initval) { \
5449989Sbostic 	for (i = initval; (j = i * 2) <= nmemb; i = j) { \
5549989Sbostic 		p = (char *)bot + j * size; \
5649989Sbostic 		if (j < nmemb && compar(p, p + size) < 0) { \
5749989Sbostic 			p += size; \
5849989Sbostic 			++j; \
5949989Sbostic 		} \
6049989Sbostic 		t = (char *)bot + i * size; \
61*51173Sbostic 		if (compar(p, t) <= 0) \
6249989Sbostic 			break; \
6349989Sbostic 		SWAP(t, p); \
6449989Sbostic 	} \
6549989Sbostic }
6649989Sbostic 
6749989Sbostic /*
6850789Selan  * Select the top of the heap and 'heapify'.  Since by far the most expensive
69*51173Sbostic  * 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  *
75*51173Sbostic  * Then, starting from the *bottom* of the heap, finding k's correct place,
76*51173Sbostic  * again maintianing the invariant.  As a result of the invariant no element
77*51173Sbostic  * 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*51173Sbostic #define SELECT { \
83*51173Sbostic 	for (i = 1; (j = i * 2) <= nmemb; i = j) { \
8450789Selan 		p = (char *)bot + j * size; \
8550789Selan 		if (j < nmemb && compar(p, p + size) < 0) { \
8650789Selan 			p += size; \
8750789Selan 			++j; \
8850789Selan 		} \
8950789Selan 		t = (char *)bot + i * size; \
90*51173Sbostic 		COPY(t, p); \
9150789Selan 	} \
92*51173Sbostic 	for (;;) { \
9350789Selan 		j = i; \
9450789Selan 		i = j / 2; \
9550789Selan 		p = (char *)bot + j * size; \
9650789Selan 		t = (char *)bot + i * size; \
97*51173Sbostic 		if (j == 1 || compar(k, t) < 0) { \
98*51173Sbostic 			COPY(p, k); \
9950789Selan 			break; \
10050789Selan 		} \
101*51173Sbostic 		COPY(p, t); \
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
110*51173Sbostic  * only advantage over quicksort is that it requires little additional memory.
11149989Sbostic  */
11250789Selan int
11349989Sbostic heapsort(bot, nmemb, size, compar)
114*51173Sbostic 	void *bot;
115*51173Sbostic 	size_t nmemb, size;
116*51173Sbostic 	int (*compar) __P((const void *, const void *));
11749989Sbostic {
118*51173Sbostic 	register int cnt, i, j, l;
119*51173Sbostic 	register char ch, *t1, *t2;
120*51173Sbostic 	char *k, *p, *t;
12149989Sbostic 
12249989Sbostic 	if (nmemb <= 1)
12349989Sbostic 		return (0);
124*51173Sbostic 
12549989Sbostic 	if (!size) {
12649989Sbostic 		errno = EINVAL;
12749989Sbostic 		return (-1);
12849989Sbostic 	}
129*51173Sbostic 
130*51173Sbostic 	if ((k = malloc(size)) == NULL)
131*51173Sbostic 		return (-1);
132*51173Sbostic 
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;)
14050789Selan 		CREATE(l);
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*51173Sbostic 		COPY(k, (char *)bot + nmemb * size);
149*51173Sbostic 		COPY((char *)bot + nmemb * size, (char *)bot + size);
15049989Sbostic 		--nmemb;
151*51173Sbostic 		SELECT;
15249989Sbostic 	}
153*51173Sbostic 	free(k);
15449989Sbostic 	return (0);
15549989Sbostic }
156