heapsort(3bsd)
π heapsort(3bsd) β like, the LOCAL bestie manual π
okay so welcome to the glow-up of heapsort(3bsd), a LOCAL manual page, and bestie we are about to sort some stuff. no cap. π¦
β¨ NAME β¨
heapsort, mergesort β sort functions π
π LIBRARY π
Utility functions from BSD systems (libbsd, -lbsd) π
πΈ SYNOPSIS πΈ
#include <stdlib.h>
(See libbsd(7) for include usage.)
int
heapsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
int
mergesort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
π« DESCRIPTION π«
so heapsort() is a modified selection sort, and mergesort() is a modified merge sort with exponential search β and the whole point of it is sorting data that already has some pre-existing order to it. it's giving β¨organizedβ¨.
heapsort() sorts an array of nmemb objects, and the initial member of that array is pointed to by base. the size of each object? that's size, obviously. mergesort() behaves similarly, BUT (and this is the plot twist π) it requires that size be greater than "sizeof(void *) / 2".
the contents of the array base get sorted in ascending order according to a comparison function pointed to by compar, which requires two arguments pointing to the objects being compared. simple as that bestie.
that comparison function must return an integer that's less than, equal to, or greater than zero, depending on whether the first argument is considered respectively less than, equal to, or greater than the second. no vibes-based returns allowed, we need numbers. π€
now here's the tea: the algorithm implemented by heapsort() is not stable β meaning if two members compare as equal, their order in the sorted array is undefined. like, nobody knows, not even the algorithm. π meanwhile mergesort() is stable. queen behavior. π
heapsort() is an implementation of J.W.J. William's "heapsort" algorithm, a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. heapsort takes O N lg N worst-case time. its only advantage over qsort() is that it uses almost no additional memory; while qsort() does not allocate memory, it is implemented using recursion. so like, it's the low-maintenance friend. π«Ά
mergesort() requires additional memory of size nmemb * size bytes β it should be used only when space is not at a premium. it's optimized for data with pre-existing order; its worst case time is O N lg N; its best case is O N. glow-up in the best case fr fr. π₯
normally, qsort() is faster than mergesort() is faster than heapsort(). but memory availability and pre-existing order in the data can make this untrue. so don't come for me if your benchmarks say otherwise. π
reminder bestie: that speed ranking is the normal case, not a promise. memory and pre-existing order can flip the whole thing.
π RETURN VALUES π
heapsort() and mergesort() return the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error. iconic success, tragic failure, no in-between. β¨
π ERRORS π
heapsort() and mergesort() succeed unless:
[EINVAL]β Thesizeargument is zero, or, thesizeargument tomergesort()is less than "sizeof(void *) / 2". like girl, that size is not it. π[ENOMEM]β Theheapsort()ormergesort()functions were unable to allocate memory. the memory said "no π ".
π SEE ALSO π
sort(1),radixsort(3bsd)- Williams, J.W.J, "Heapsort", Communications of the ACM, 7:1, pp. 347-348, 1964.
- Knuth, D.E., "Sorting and Searching", The Art of Computer Programming, Vol. 3, pp. 114-123, 145-149, 1968.
- McIlroy, P.M., "Optimistic Sorting and Information Theoretic Complexity", Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, January 1992.
- Bentley, J.L. and McIlroy, M.D., "Engineering a Sort Function", Software--Practice and Experience, Vol. 23(11), pp. 1249-1265, November 1993.
and that's the whole manual bestie, periodt. this page is from the Debian crew, dated September 30, 2003, and it's heapsort(3bsd) β go forth and sort responsibly. mwah. πβ¨π
π¦ where this page comes from
- alpine 3.22: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.23: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.24: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- arch rolling: libbsd 0.12.2-2 Β· custom
- debian bookworm: libbsd-dev 0.11.7-2
- debian trixie: libbsd-dev 0.12.2-2
- epel 10: libbsd-devel 0.12.2-4.el10_0 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- epel 9: libbsd-devel 0.12.2-1.el9 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- nixos 26.05: libbsd 0.12.2
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- ubuntu noble: libbsd-dev 0.12.1-1build1
- ubuntu noble: libbsd-dev 0.12.1-1build1.1
- ubuntu questing: libbsd-dev 0.12.2-2build1
- ubuntu resolute: libbsd-dev 0.12.2-2build2
- ubuntu stonking: libbsd-dev 0.12.2-3
- Copyright (c) 1990, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms