xref: /openbsd/lib/libc/stdlib/heapsort.c (revision d415bd75)
1 /*	$OpenBSD: heapsort.c,v 1.11 2017/05/20 12:48:56 millert Exp $ */
2 /*-
3  * Copyright (c) 1991, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/types.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 
38 /*
39  * Swap two areas of size number of bytes.  Although qsort(3) permits random
40  * blocks of memory to be sorted, sorting pointers is almost certainly the
41  * common case (and, were it not, could easily be made so).  Regardless, it
42  * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer
43  * arithmetic gets lost in the time required for comparison function calls.
44  */
45 #define	SWAP(a, b, count, size, tmp) { \
46 	count = size; \
47 	do { \
48 		tmp = *a; \
49 		*a++ = *b; \
50 		*b++ = tmp; \
51 	} while (--count); \
52 }
53 
54 /* Copy one block of size size to another. */
55 #define COPY(a, b, count, size, tmp1, tmp2) { \
56 	count = size; \
57 	tmp1 = a; \
58 	tmp2 = b; \
59 	do { \
60 		*tmp1++ = *tmp2++; \
61 	} while (--count); \
62 }
63 
64 /*
65  * Build the list into a heap, where a heap is defined such that for
66  * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N.
67  *
68  * There are two cases.  If j == nmemb, select largest of Ki and Kj.  If
69  * j < nmemb, select largest of Ki, Kj and Kj+1.
70  */
71 #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \
72 	for (par_i = initval; (child_i = par_i * 2) <= nmemb; \
73 	    par_i = child_i) { \
74 		child = base + child_i * size; \
75 		if (child_i < nmemb && compar(child, child + size) < 0) { \
76 			child += size; \
77 			++child_i; \
78 		} \
79 		par = base + par_i * size; \
80 		if (compar(child, par) <= 0) \
81 			break; \
82 		SWAP(par, child, count, size, tmp); \
83 	} \
84 }
85 
86 /*
87  * Select the top of the heap and 'heapify'.  Since by far the most expensive
88  * action is the call to the compar function, a considerable optimization
89  * in the average case can be achieved due to the fact that k, the displaced
90  * element, is usually quite small, so it would be preferable to first
91  * heapify, always maintaining the invariant that the larger child is copied
92  * over its parent's record.
93  *
94  * Then, starting from the *bottom* of the heap, finding k's correct place,
95  * again maintaining the invariant.  As a result of the invariant no element
96  * is 'lost' when k is assigned its correct place in the heap.
97  *
98  * The time savings from this optimization are on the order of 15-20% for the
99  * average case. See Knuth, Vol. 3, page 158, problem 18.
100  *
101  * XXX Don't break the #define SELECT line, below.  Reiser cpp gets upset.
102  */
103 #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \
104 	for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \
105 		child = base + child_i * size; \
106 		if (child_i < nmemb && compar(child, child + size) < 0) { \
107 			child += size; \
108 			++child_i; \
109 		} \
110 		par = base + par_i * size; \
111 		COPY(par, child, count, size, tmp1, tmp2); \
112 	} \
113 	for (;;) { \
114 		child_i = par_i; \
115 		par_i = child_i / 2; \
116 		child = base + child_i * size; \
117 		par = base + par_i * size; \
118 		if (child_i == 1 || compar(k, par) < 0) { \
119 			COPY(child, k, count, size, tmp1, tmp2); \
120 			break; \
121 		} \
122 		COPY(child, par, count, size, tmp1, tmp2); \
123 	} \
124 }
125 
126 /*
127  * Heapsort -- Knuth, Vol. 3, page 145.  Runs in O (N lg N), both average
128  * and worst.  While heapsort is faster than the worst case of quicksort,
129  * the BSD quicksort does median selection so that the chance of finding
130  * a data set that will trigger the worst case is nonexistent.  Heapsort's
131  * only advantage over quicksort is that it requires little additional memory.
132  */
133 int
134 heapsort(void *vbase, size_t nmemb, size_t size,
135     int (*compar)(const void *, const void *))
136 {
137 	size_t cnt, i, j, l;
138 	char tmp, *tmp1, *tmp2;
139 	char *base, *k, *p, *t;
140 
141 	if (nmemb <= 1)
142 		return (0);
143 
144 	if (!size) {
145 		errno = EINVAL;
146 		return (-1);
147 	}
148 
149 	if ((k = malloc(size)) == NULL)
150 		return (-1);
151 
152 	/*
153 	 * Items are numbered from 1 to nmemb, so offset from size bytes
154 	 * below the starting address.
155 	 */
156 	base = (char *)vbase - size;
157 
158 	for (l = nmemb / 2 + 1; --l;)
159 		CREATE(l, nmemb, i, j, t, p, size, cnt, tmp);
160 
161 	/*
162 	 * For each element of the heap, save the largest element into its
163 	 * final slot, save the displaced element (k), then recreate the
164 	 * heap.
165 	 */
166 	while (nmemb > 1) {
167 		COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2);
168 		COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2);
169 		--nmemb;
170 		SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2);
171 	}
172 	free(k);
173 	return (0);
174 }
175 DEF_WEAK(heapsort);
176