xref: /csrg-svn/lib/libc/stdlib/heapsort.c (revision 50789)
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*50789Selan static char sccsid[] = "@(#)heapsort.c	1.3 (Berkeley) 7/29/91";
1349989Sbostic #endif /* LIBC_SCCS and not lint */
1449989Sbostic 
1549989Sbostic #include <sys/cdefs.h>
1649989Sbostic #include <sys/types.h>
1749989Sbostic #include <errno.h>
1849989Sbostic #include <stdlib.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*50789Selan 	int cnt = size; \
29*50789Selan 	char	ch; \
3049989Sbostic 	do { \
3149989Sbostic 		ch = *a; \
3249989Sbostic 		*a++ = *b; \
3349989Sbostic 		*b++ = ch; \
3449989Sbostic 	} while (--cnt); \
3549989Sbostic }
3649989Sbostic 
3749989Sbostic /*
38*50789Selan  * Assign one block of size size to another.
39*50789Selan  */
40*50789Selan 
41*50789Selan #define ASSIGN(a,b) { \
42*50789Selan 	int cnt = size; \
43*50789Selan 	char *t1 = a, *t2 = b; \
44*50789Selan 	do { \
45*50789Selan 		*t1++ = *t2++; \
46*50789Selan 	} while (--cnt); \
47*50789Selan }
48*50789Selan 
49*50789Selan 
50*50789Selan /*
5149989Sbostic  * Build the list into a heap, where a heap is defined such that for
5249989Sbostic  * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N.
5349989Sbostic  *
5449989Sbostic  * There two cases.  If j == nmemb, select largest of Ki and Kj.  If
5549989Sbostic  * j < nmemb, select largest of Ki, Kj and Kj+1.
5649989Sbostic  *
5749989Sbostic  */
58*50789Selan #define CREATE(initval) { \
59*50789Selan 	int i,j; \
60*50789Selan 	char *t,*p; \
6149989Sbostic 	for (i = initval; (j = i * 2) <= nmemb; i = j) { \
6249989Sbostic 		p = (char *)bot + j * size; \
6349989Sbostic 		if (j < nmemb && compar(p, p + size) < 0) { \
6449989Sbostic 			p += size; \
6549989Sbostic 			++j; \
6649989Sbostic 		} \
6749989Sbostic 		t = (char *)bot + i * size; \
68*50789Selan 		if (compar(p,t) <= 0) \
6949989Sbostic 			break; \
7049989Sbostic 		SWAP(t, p); \
7149989Sbostic 	} \
7249989Sbostic }
7349989Sbostic 
7449989Sbostic /*
75*50789Selan  * Select the top of the heap and 'heapify'.  Since by far the most expensive
76*50789Selan  * action is the call to the compar function, an considerable optimization
77*50789Selan  * in the average case can be achieved due to the fact that k, the displaced
78*50789Selan  * elememt, is ususally quite small, so it would be preferable to first
79*50789Selan  * heapify, always maintaining the invariant that the larger child is copied
80*50789Selan  * over its parent's record.
81*50789Selan  *
82*50789Selan  * Then, starting from the *bottom* of the heap, finding k's correct
83*50789Selan  * place, again maintianing the invariant.  As a result of the invariant
84*50789Selan  * no element is 'lost' when k is assigned it's correct place in the heap.
85*50789Selan  *
86*50789Selan  * The time savings from this optimization are on the order of 15-20% for the
87*50789Selan  * average case. See Knuth, Vol. 3, page 158, problem 18.
88*50789Selan  */
89*50789Selan 
90*50789Selan #define SELECT(initval) { \
91*50789Selan 	int	i,j; \
92*50789Selan 	char	*p,*t; \
93*50789Selan 	for (i = initval; (j = i * 2) <= nmemb; i = j) { \
94*50789Selan 		p = (char *)bot + j * size; \
95*50789Selan 		if (j < nmemb && compar(p, p + size) < 0) { \
96*50789Selan 			p += size; \
97*50789Selan 			++j; \
98*50789Selan 		} \
99*50789Selan 		t = (char *)bot + i * size; \
100*50789Selan 		ASSIGN(t, p); \
101*50789Selan 	} \
102*50789Selan 	while (1) { \
103*50789Selan 		j = i; \
104*50789Selan 		i = j / 2; \
105*50789Selan 		p = (char *)bot + j * size; \
106*50789Selan 		t = (char *)bot + i * size; \
107*50789Selan 		if ( j == initval || compar(k, t) < 0) { \
108*50789Selan 			ASSIGN(p, k); \
109*50789Selan 			break; \
110*50789Selan 		} \
111*50789Selan 		ASSIGN(p, t); \
112*50789Selan 	} \
113*50789Selan }
114*50789Selan 
115*50789Selan /*
11649989Sbostic  * Heapsort -- Knuth, Vol. 3, page 145.  Runs in O (N lg N), both average
11749989Sbostic  * and worst.  While heapsort is faster than the worst case of quicksort,
11849989Sbostic  * the BSD quicksort does median selection so that the chance of finding
11949989Sbostic  * a data set that will trigger the worst case is nonexistent.  Heapsort's
12049989Sbostic  * only advantage over quicksort is that it requires no additional memory.
12149989Sbostic  */
122*50789Selan int
12349989Sbostic heapsort(bot, nmemb, size, compar)
124*50789Selan 	void   *bot;
125*50789Selan 	size_t  nmemb, size;
126*50789Selan 	int     (*compar) __P((const void *, const void *));
12749989Sbostic {
128*50789Selan 	char   *p, *t, *k = (char *) malloc(size);
129*50789Selan 	int     l;
13049989Sbostic 
13149989Sbostic 	if (nmemb <= 1)
13249989Sbostic 		return (0);
13349989Sbostic 	if (!size) {
13449989Sbostic 		errno = EINVAL;
13549989Sbostic 		return (-1);
13649989Sbostic 	}
13749989Sbostic 	/*
13849989Sbostic 	 * Items are numbered from 1 to nmemb, so offset from size bytes
13949989Sbostic 	 * below the starting address.
14049989Sbostic 	 */
14149989Sbostic 	bot -= size;
14249989Sbostic 
14349989Sbostic 	for (l = nmemb / 2 + 1; --l;)
144*50789Selan 		CREATE(l);
14549989Sbostic 
14649989Sbostic 	/*
14749989Sbostic 	 * For each element of the heap, save the largest element into its
148*50789Selan 	 * final slot, save the displaced element (k), then recreate the
149*50789Selan 	 * heap.
15049989Sbostic 	 */
15149989Sbostic 	while (nmemb > 1) {
152*50789Selan 		p = (char *) bot + size;
153*50789Selan 		t = (char *) bot + nmemb * size;
154*50789Selan 		ASSIGN(k, t);
155*50789Selan 		ASSIGN(t, p);
15649989Sbostic 		--nmemb;
157*50789Selan 		SELECT(1);
15849989Sbostic 	}
15949989Sbostic 	return (0);
16049989Sbostic }
161