149989Sbostic /*-
2*61180Sbostic * Copyright (c) 1991, 1993
3*61180Sbostic * The Regents of the University of California. All rights reserved.
449989Sbostic *
550466Sbostic * This code is derived from software contributed to Berkeley by
650466Sbostic * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias.
750466Sbostic *
849989Sbostic * %sccs.include.redist.c%
949989Sbostic */
1049989Sbostic
1149989Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*61180Sbostic static char sccsid[] = "@(#)heapsort.c 8.1 (Berkeley) 06/04/93";
1349989Sbostic #endif /* LIBC_SCCS and not lint */
1449989Sbostic
1549989Sbostic #include <sys/types.h>
1649989Sbostic #include <errno.h>
1749989Sbostic #include <stdlib.h>
1851173Sbostic #include <stddef.h>
1949989Sbostic
2049989Sbostic /*
2149989Sbostic * Swap two areas of size number of bytes. Although qsort(3) permits random
2249989Sbostic * blocks of memory to be sorted, sorting pointers is almost certainly the
2349989Sbostic * common case (and, were it not, could easily be made so). Regardless, it
2449989Sbostic * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer
2549989Sbostic * arithmetic gets lost in the time required for comparison function calls.
2649989Sbostic */
2751206Selan #define SWAP(a, b, count, size, tmp) { \
2851206Selan count = size; \
2949989Sbostic do { \
3051206Selan tmp = *a; \
3149989Sbostic *a++ = *b; \
3251206Selan *b++ = tmp; \
3351206Selan } while (--count); \
3449989Sbostic }
3549989Sbostic
3651173Sbostic /* Copy one block of size size to another. */
3751206Selan #define COPY(a, b, count, size, tmp1, tmp2) { \
3851206Selan count = size; \
3951206Selan tmp1 = a; \
4051206Selan tmp2 = b; \
4150789Selan do { \
4251206Selan *tmp1++ = *tmp2++; \
4351206Selan } while (--count); \
4450789Selan }
4550789Selan
4650789Selan /*
4749989Sbostic * Build the list into a heap, where a heap is defined such that for
4849989Sbostic * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N.
4949989Sbostic *
5049989Sbostic * There two cases. If j == nmemb, select largest of Ki and Kj. If
5149989Sbostic * j < nmemb, select largest of Ki, Kj and Kj+1.
5249989Sbostic */
5351206Selan #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \
5451207Sbostic for (par_i = initval; (child_i = par_i * 2) <= nmemb; \
5551207Sbostic par_i = child_i) { \
5651653Sbostic child = base + child_i * size; \
5751206Selan if (child_i < nmemb && compar(child, child + size) < 0) { \
5851206Selan child += size; \
5951206Selan ++child_i; \
6049989Sbostic } \
6151653Sbostic par = base + par_i * size; \
6251206Selan if (compar(child, par) <= 0) \
6349989Sbostic break; \
6451206Selan SWAP(par, child, count, size, tmp); \
6549989Sbostic } \
6649989Sbostic }
6749989Sbostic
6849989Sbostic /*
6950789Selan * Select the top of the heap and 'heapify'. Since by far the most expensive
7051173Sbostic * action is the call to the compar function, a considerable optimization
7150789Selan * in the average case can be achieved due to the fact that k, the displaced
7250789Selan * elememt, is ususally quite small, so it would be preferable to first
7350789Selan * heapify, always maintaining the invariant that the larger child is copied
7450789Selan * over its parent's record.
7550789Selan *
7651173Sbostic * Then, starting from the *bottom* of the heap, finding k's correct place,
7751173Sbostic * again maintianing the invariant. As a result of the invariant no element
7851173Sbostic * is 'lost' when k is assigned its correct place in the heap.
7950789Selan *
8050789Selan * The time savings from this optimization are on the order of 15-20% for the
8150789Selan * average case. See Knuth, Vol. 3, page 158, problem 18.
8251653Sbostic *
8351653Sbostic * XXX Don't break the #define SELECT line, below. Reiser cpp gets upset.
8450789Selan */
8551653Sbostic #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \
8651206Selan for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \
8751653Sbostic child = base + child_i * size; \
8851206Selan if (child_i < nmemb && compar(child, child + size) < 0) { \
8951206Selan child += size; \
9051206Selan ++child_i; \
9150789Selan } \
9251653Sbostic par = base + par_i * size; \
9351206Selan COPY(par, child, count, size, tmp1, tmp2); \
9450789Selan } \
9551173Sbostic for (;;) { \
9651206Selan child_i = par_i; \
9751206Selan par_i = child_i / 2; \
9851653Sbostic child = base + child_i * size; \
9951653Sbostic par = base + par_i * size; \
10051206Selan if (child_i == 1 || compar(k, par) < 0) { \
10151206Selan COPY(child, k, count, size, tmp1, tmp2); \
10250789Selan break; \
10350789Selan } \
10451206Selan COPY(child, par, count, size, tmp1, tmp2); \
10550789Selan } \
10650789Selan }
10750789Selan
10850789Selan /*
10949989Sbostic * Heapsort -- Knuth, Vol. 3, page 145. Runs in O (N lg N), both average
11049989Sbostic * and worst. While heapsort is faster than the worst case of quicksort,
11149989Sbostic * the BSD quicksort does median selection so that the chance of finding
11249989Sbostic * a data set that will trigger the worst case is nonexistent. Heapsort's
11351173Sbostic * only advantage over quicksort is that it requires little additional memory.
11449989Sbostic */
11550789Selan int
heapsort(vbase,nmemb,size,compar)11651653Sbostic heapsort(vbase, nmemb, size, compar)
11751653Sbostic void *vbase;
11851173Sbostic size_t nmemb, size;
11951173Sbostic int (*compar) __P((const void *, const void *));
12049989Sbostic {
12151173Sbostic register int cnt, i, j, l;
12251206Selan register char tmp, *tmp1, *tmp2;
12351653Sbostic char *base, *k, *p, *t;
12449989Sbostic
12549989Sbostic if (nmemb <= 1)
12649989Sbostic return (0);
12751173Sbostic
12849989Sbostic if (!size) {
12949989Sbostic errno = EINVAL;
13049989Sbostic return (-1);
13149989Sbostic }
13251173Sbostic
13351173Sbostic if ((k = malloc(size)) == NULL)
13451173Sbostic return (-1);
13551173Sbostic
13649989Sbostic /*
13749989Sbostic * Items are numbered from 1 to nmemb, so offset from size bytes
13849989Sbostic * below the starting address.
13949989Sbostic */
14051653Sbostic base = (char *)vbase - size;
14149989Sbostic
14249989Sbostic for (l = nmemb / 2 + 1; --l;)
14351206Selan CREATE(l, nmemb, i, j, t, p, size, cnt, tmp);
14449989Sbostic
14549989Sbostic /*
14649989Sbostic * For each element of the heap, save the largest element into its
14750789Selan * final slot, save the displaced element (k), then recreate the
14850789Selan * heap.
14949989Sbostic */
15049989Sbostic while (nmemb > 1) {
15151653Sbostic COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2);
15251653Sbostic COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2);
15349989Sbostic --nmemb;
15451206Selan SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2);
15549989Sbostic }
15651173Sbostic free(k);
15749989Sbostic return (0);
15849989Sbostic }
159