heapsort.c revision 1573
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 <sys/types.h>
421573Srgrimes#include <errno.h>
431573Srgrimes#include <stdlib.h>
441573Srgrimes#include <stddef.h>
451573Srgrimes
461573Srgrimes/*
471573Srgrimes * Swap two areas of size number of bytes.  Although qsort(3) permits random
481573Srgrimes * blocks of memory to be sorted, sorting pointers is almost certainly the
491573Srgrimes * common case (and, were it not, could easily be made so).  Regardless, it
501573Srgrimes * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer
511573Srgrimes * arithmetic gets lost in the time required for comparison function calls.
521573Srgrimes */
531573Srgrimes#define	SWAP(a, b, count, size, tmp) { \
541573Srgrimes	count = size; \
551573Srgrimes	do { \
561573Srgrimes		tmp = *a; \
571573Srgrimes		*a++ = *b; \
581573Srgrimes		*b++ = tmp; \
591573Srgrimes	} while (--count); \
601573Srgrimes}
611573Srgrimes
621573Srgrimes/* Copy one block of size size to another. */
631573Srgrimes#define COPY(a, b, count, size, tmp1, tmp2) { \
641573Srgrimes	count = size; \
651573Srgrimes	tmp1 = a; \
661573Srgrimes	tmp2 = b; \
671573Srgrimes	do { \
681573Srgrimes		*tmp1++ = *tmp2++; \
691573Srgrimes	} while (--count); \
701573Srgrimes}
711573Srgrimes
721573Srgrimes/*
731573Srgrimes * Build the list into a heap, where a heap is defined such that for
741573Srgrimes * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N.
751573Srgrimes *
761573Srgrimes * There two cases.  If j == nmemb, select largest of Ki and Kj.  If
771573Srgrimes * j < nmemb, select largest of Ki, Kj and Kj+1.
781573Srgrimes */
791573Srgrimes#define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \
801573Srgrimes	for (par_i = initval; (child_i = par_i * 2) <= nmemb; \
811573Srgrimes	    par_i = child_i) { \
821573Srgrimes		child = base + child_i * size; \
831573Srgrimes		if (child_i < nmemb && compar(child, child + size) < 0) { \
841573Srgrimes			child += size; \
851573Srgrimes			++child_i; \
861573Srgrimes		} \
871573Srgrimes		par = base + par_i * size; \
881573Srgrimes		if (compar(child, par) <= 0) \
891573Srgrimes			break; \
901573Srgrimes		SWAP(par, child, count, size, tmp); \
911573Srgrimes	} \
921573Srgrimes}
931573Srgrimes
941573Srgrimes/*
951573Srgrimes * Select the top of the heap and 'heapify'.  Since by far the most expensive
961573Srgrimes * action is the call to the compar function, a considerable optimization
971573Srgrimes * in the average case can be achieved due to the fact that k, the displaced
981573Srgrimes * elememt, is ususally quite small, so it would be preferable to first
991573Srgrimes * heapify, always maintaining the invariant that the larger child is copied
1001573Srgrimes * over its parent's record.
1011573Srgrimes *
1021573Srgrimes * Then, starting from the *bottom* of the heap, finding k's correct place,
1031573Srgrimes * again maintianing the invariant.  As a result of the invariant no element
1041573Srgrimes * is 'lost' when k is assigned its correct place in the heap.
1051573Srgrimes *
1061573Srgrimes * The time savings from this optimization are on the order of 15-20% for the
1071573Srgrimes * average case. See Knuth, Vol. 3, page 158, problem 18.
1081573Srgrimes *
1091573Srgrimes * XXX Don't break the #define SELECT line, below.  Reiser cpp gets upset.
1101573Srgrimes */
1111573Srgrimes#define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \
1121573Srgrimes	for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \
1131573Srgrimes		child = base + child_i * size; \
1141573Srgrimes		if (child_i < nmemb && compar(child, child + size) < 0) { \
1151573Srgrimes			child += size; \
1161573Srgrimes			++child_i; \
1171573Srgrimes		} \
1181573Srgrimes		par = base + par_i * size; \
1191573Srgrimes		COPY(par, child, count, size, tmp1, tmp2); \
1201573Srgrimes	} \
1211573Srgrimes	for (;;) { \
1221573Srgrimes		child_i = par_i; \
1231573Srgrimes		par_i = child_i / 2; \
1241573Srgrimes		child = base + child_i * size; \
1251573Srgrimes		par = base + par_i * size; \
1261573Srgrimes		if (child_i == 1 || compar(k, par) < 0) { \
1271573Srgrimes			COPY(child, k, count, size, tmp1, tmp2); \
1281573Srgrimes			break; \
1291573Srgrimes		} \
1301573Srgrimes		COPY(child, par, count, size, tmp1, tmp2); \
1311573Srgrimes	} \
1321573Srgrimes}
1331573Srgrimes
1341573Srgrimes/*
1351573Srgrimes * Heapsort -- Knuth, Vol. 3, page 145.  Runs in O (N lg N), both average
1361573Srgrimes * and worst.  While heapsort is faster than the worst case of quicksort,
1371573Srgrimes * the BSD quicksort does median selection so that the chance of finding
1381573Srgrimes * a data set that will trigger the worst case is nonexistent.  Heapsort's
1391573Srgrimes * only advantage over quicksort is that it requires little additional memory.
1401573Srgrimes */
1411573Srgrimesint
1421573Srgrimesheapsort(vbase, nmemb, size, compar)
1431573Srgrimes	void *vbase;
1441573Srgrimes	size_t nmemb, size;
1451573Srgrimes	int (*compar) __P((const void *, const void *));
1461573Srgrimes{
1471573Srgrimes	register int cnt, i, j, l;
1481573Srgrimes	register char tmp, *tmp1, *tmp2;
1491573Srgrimes	char *base, *k, *p, *t;
1501573Srgrimes
1511573Srgrimes	if (nmemb <= 1)
1521573Srgrimes		return (0);
1531573Srgrimes
1541573Srgrimes	if (!size) {
1551573Srgrimes		errno = EINVAL;
1561573Srgrimes		return (-1);
1571573Srgrimes	}
1581573Srgrimes
1591573Srgrimes	if ((k = malloc(size)) == NULL)
1601573Srgrimes		return (-1);
1611573Srgrimes
1621573Srgrimes	/*
1631573Srgrimes	 * Items are numbered from 1 to nmemb, so offset from size bytes
1641573Srgrimes	 * below the starting address.
1651573Srgrimes	 */
1661573Srgrimes	base = (char *)vbase - size;
1671573Srgrimes
1681573Srgrimes	for (l = nmemb / 2 + 1; --l;)
1691573Srgrimes		CREATE(l, nmemb, i, j, t, p, size, cnt, tmp);
1701573Srgrimes
1711573Srgrimes	/*
1721573Srgrimes	 * For each element of the heap, save the largest element into its
1731573Srgrimes	 * final slot, save the displaced element (k), then recreate the
1741573Srgrimes	 * heap.
1751573Srgrimes	 */
1761573Srgrimes	while (nmemb > 1) {
1771573Srgrimes		COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2);
1781573Srgrimes		COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2);
1791573Srgrimes		--nmemb;
1801573Srgrimes		SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2);
1811573Srgrimes	}
1821573Srgrimes	free(k);
1831573Srgrimes	return (0);
1841573Srgrimes}
185