1heapsort(3bsd) LOCAL heapsort(3bsd)
2
4 heapsort, mergesort — sort functions
5
7 Utility functions from BSD systems (libbsd, -lbsd)
8
10 #include <stdlib.h>
11 (See libbsd(7) for include usage.)
12
13 int
14 heapsort(void *base, size_t nmemb, size_t size,
15 int (*compar)(const void *, const void *));
16
17 int
18 mergesort(void *base, size_t nmemb, size_t size,
19 int (*compar)(const void *, const void *));
20
22 The heapsort() function is a modified selection sort. The mergesort()
23 function is a modified merge sort with exponential search intended for
24 sorting data with pre-existing order.
25
26 The heapsort() function sorts an array of nmemb objects, the initial mem‐
27 ber of which is pointed to by base. The size of each object is specified
28 by size. The mergesort() function behaves similarly, but requires that
29 size be greater than “sizeof(void *) / 2”.
30
31 The contents of the array base are sorted in ascending order according to
32 a comparison function pointed to by compar, which requires two arguments
33 pointing to the objects being compared.
34
35 The comparison function must return an integer less than, equal to, or
36 greater than zero if the first argument is considered to be respectively
37 less than, equal to, or greater than the second.
38
39 The algorithm implemented by heapsort() is not stable, that is, if two
40 members compare as equal, their order in the sorted array is undefined.
41 The mergesort() algorithm is stable.
42
43 The heapsort() function is an implementation of J.W.J. William's
44 “heapsort” algorithm, a variant of selection sorting; in particular, see
45 D.E. Knuth's Algorithm H. Heapsort takes O N lg N worst-case time. Its
46 only advantage over qsort() is that it uses almost no additional memory;
47 while qsort() does not allocate memory, it is implemented using recur‐
48 sion.
49
50 The function mergesort() requires additional memory of size nmemb * size
51 bytes; it should be used only when space is not at a premium. The
52 mergesort() function is optimized for data with pre-existing order; its
53 worst case time is O N lg N; its best case is O N.
54
55 Normally, qsort() is faster than mergesort() is faster than heapsort().
56 Memory availability and pre-existing order in the data can make this un‐
57 true.
58
60 The heapsort() and mergesort() functions return the value 0 if success‐
61 ful; otherwise the value -1 is returned and the global variable errno is
62 set to indicate the error.
63
65 The heapsort() and mergesort() functions succeed unless:
66
67 [EINVAL] The size argument is zero, or, the size argument to
68 mergesort() is less than “sizeof(void *) / 2”.
69
70 [ENOMEM] The heapsort() or mergesort() functions were unable to
71 allocate memory.
72
74 sort(1), radixsort(3bsd)
75
76 Williams, J.W.J, “Heapsort”, Communications of the ACM, 7:1, pp. 347-348,
77 1964.
78
79 Knuth, D.E., “Sorting and Searching”, The Art of Computer Programming,
80 Vol. 3, pp. 114-123, 145-149, 1968.
81
82 McIlroy, P.M., “Optimistic Sorting and Information Theoretic Complexity”,
83 Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, January 1992.
84
85 Bentley, J.L. and McIlroy, M.D., “Engineering a Sort Function”,
86 Software--Practice and Experience, Vol. 23(11), pp. 1249-1265,
87 November 1993.
88
89BSD September 30, 2003 BSD