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