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