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