heapsort.c revision 15312
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1991, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. All advertising materials mentioning features or use of this software
171573Srgrimes *    must display the following acknowledgement:
181573Srgrimes *	This product includes software developed by the University of
191573Srgrimes *	California, Berkeley and its contributors.
201573Srgrimes * 4. Neither the name of the University nor the names of its contributors
211573Srgrimes *    may be used to endorse or promote products derived from this software
221573Srgrimes *    without specific prior written permission.
231573Srgrimes *
241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341573Srgrimes * SUCH DAMAGE.
351573Srgrimes */
361573Srgrimes
371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
381573Srgrimesstatic char sccsid[] = "@(#)heapsort.c	8.1 (Berkeley) 6/4/93";
391573Srgrimes#endif /* LIBC_SCCS and not lint */
401573Srgrimes
411573Srgrimes#include <errno.h>
4215312Sbde#include <stddef.h>
431573Srgrimes#include <stdlib.h>
441573Srgrimes
451573Srgrimes/*
461573Srgrimes * Swap two areas of size number of bytes.  Although qsort(3) permits random
471573Srgrimes * blocks of memory to be sorted, sorting pointers is almost certainly the
481573Srgrimes * common case (and, were it not, could easily be made so).  Regardless, it
491573Srgrimes * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer
501573Srgrimes * arithmetic gets lost in the time required for comparison function calls.
511573Srgrimes */
521573Srgrimes#define	SWAP(a, b, count, size, tmp) { \
531573Srgrimes	count = size; \
541573Srgrimes	do { \
551573Srgrimes		tmp = *a; \
561573Srgrimes		*a++ = *b; \
571573Srgrimes		*b++ = tmp; \
581573Srgrimes	} while (--count); \
591573Srgrimes}
601573Srgrimes
611573Srgrimes/* Copy one block of size size to another. */
621573Srgrimes#define COPY(a, b, count, size, tmp1, tmp2) { \
631573Srgrimes	count = size; \
641573Srgrimes	tmp1 = a; \
651573Srgrimes	tmp2 = b; \
661573Srgrimes	do { \
671573Srgrimes		*tmp1++ = *tmp2++; \
681573Srgrimes	} while (--count); \
691573Srgrimes}
701573Srgrimes
711573Srgrimes/*
721573Srgrimes * Build the list into a heap, where a heap is defined such that for
731573Srgrimes * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N.
741573Srgrimes *
751573Srgrimes * There two cases.  If j == nmemb, select largest of Ki and Kj.  If
761573Srgrimes * j < nmemb, select largest of Ki, Kj and Kj+1.
771573Srgrimes */
781573Srgrimes#define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \
791573Srgrimes	for (par_i = initval; (child_i = par_i * 2) <= nmemb; \
801573Srgrimes	    par_i = child_i) { \
811573Srgrimes		child = base + child_i * size; \
821573Srgrimes		if (child_i < nmemb && compar(child, child + size) < 0) { \
831573Srgrimes			child += size; \
841573Srgrimes			++child_i; \
851573Srgrimes		} \
861573Srgrimes		par = base + par_i * size; \
871573Srgrimes		if (compar(child, par) <= 0) \
881573Srgrimes			break; \
891573Srgrimes		SWAP(par, child, count, size, tmp); \
901573Srgrimes	} \
911573Srgrimes}
921573Srgrimes
931573Srgrimes/*
941573Srgrimes * Select the top of the heap and 'heapify'.  Since by far the most expensive
951573Srgrimes * action is the call to the compar function, a considerable optimization
961573Srgrimes * in the average case can be achieved due to the fact that k, the displaced
971573Srgrimes * elememt, is ususally quite small, so it would be preferable to first
981573Srgrimes * heapify, always maintaining the invariant that the larger child is copied
991573Srgrimes * over its parent's record.
1001573Srgrimes *
1011573Srgrimes * Then, starting from the *bottom* of the heap, finding k's correct place,
1021573Srgrimes * again maintianing the invariant.  As a result of the invariant no element
1031573Srgrimes * is 'lost' when k is assigned its correct place in the heap.
1041573Srgrimes *
1051573Srgrimes * The time savings from this optimization are on the order of 15-20% for the
1061573Srgrimes * average case. See Knuth, Vol. 3, page 158, problem 18.
1071573Srgrimes *
1081573Srgrimes * XXX Don't break the #define SELECT line, below.  Reiser cpp gets upset.
1091573Srgrimes */
1101573Srgrimes#define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \
1111573Srgrimes	for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \
1121573Srgrimes		child = base + child_i * size; \
1131573Srgrimes		if (child_i < nmemb && compar(child, child + size) < 0) { \
1141573Srgrimes			child += size; \
1151573Srgrimes			++child_i; \
1161573Srgrimes		} \
1171573Srgrimes		par = base + par_i * size; \
1181573Srgrimes		COPY(par, child, count, size, tmp1, tmp2); \
1191573Srgrimes	} \
1201573Srgrimes	for (;;) { \
1211573Srgrimes		child_i = par_i; \
1221573Srgrimes		par_i = child_i / 2; \
1231573Srgrimes		child = base + child_i * size; \
1241573Srgrimes		par = base + par_i * size; \
1251573Srgrimes		if (child_i == 1 || compar(k, par) < 0) { \
1261573Srgrimes			COPY(child, k, count, size, tmp1, tmp2); \
1271573Srgrimes			break; \
1281573Srgrimes		} \
1291573Srgrimes		COPY(child, par, count, size, tmp1, tmp2); \
1301573Srgrimes	} \
1311573Srgrimes}
1321573Srgrimes
1331573Srgrimes/*
1341573Srgrimes * Heapsort -- Knuth, Vol. 3, page 145.  Runs in O (N lg N), both average
1351573Srgrimes * and worst.  While heapsort is faster than the worst case of quicksort,
1361573Srgrimes * the BSD quicksort does median selection so that the chance of finding
1371573Srgrimes * a data set that will trigger the worst case is nonexistent.  Heapsort's
1381573Srgrimes * only advantage over quicksort is that it requires little additional memory.
1391573Srgrimes */
1401573Srgrimesint
1411573Srgrimesheapsort(vbase, nmemb, size, compar)
1421573Srgrimes	void *vbase;
1431573Srgrimes	size_t nmemb, size;
1441573Srgrimes	int (*compar) __P((const void *, const void *));
1451573Srgrimes{
1461573Srgrimes	register int cnt, i, j, l;
1471573Srgrimes	register char tmp, *tmp1, *tmp2;
1481573Srgrimes	char *base, *k, *p, *t;
1491573Srgrimes
1501573Srgrimes	if (nmemb <= 1)
1511573Srgrimes		return (0);
1521573Srgrimes
1531573Srgrimes	if (!size) {
1541573Srgrimes		errno = EINVAL;
1551573Srgrimes		return (-1);
1561573Srgrimes	}
1571573Srgrimes
1581573Srgrimes	if ((k = malloc(size)) == NULL)
1591573Srgrimes		return (-1);
1601573Srgrimes
1611573Srgrimes	/*
1621573Srgrimes	 * Items are numbered from 1 to nmemb, so offset from size bytes
1631573Srgrimes	 * below the starting address.
1641573Srgrimes	 */
1651573Srgrimes	base = (char *)vbase - size;
1661573Srgrimes
1671573Srgrimes	for (l = nmemb / 2 + 1; --l;)
1681573Srgrimes		CREATE(l, nmemb, i, j, t, p, size, cnt, tmp);
1691573Srgrimes
1701573Srgrimes	/*
1711573Srgrimes	 * For each element of the heap, save the largest element into its
1721573Srgrimes	 * final slot, save the displaced element (k), then recreate the
1731573Srgrimes	 * heap.
1741573Srgrimes	 */
1751573Srgrimes	while (nmemb > 1) {
1761573Srgrimes		COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2);
1771573Srgrimes		COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2);
1781573Srgrimes		--nmemb;
1791573Srgrimes		SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2);
1801573Srgrimes	}
1811573Srgrimes	free(k);
1821573Srgrimes	return (0);
1831573Srgrimes}
184