OpenBSD manual page server

Manual Page Search Parameters

QSORT(3) Library Functions Manual QSORT(3)

qsort, heapsort, mergesortsort functions

#include <stdlib.h>

void
qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));

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 *));

The () function is a modified partition-exchange sort, or quicksort. The heapsort() function is a modified selection sort. The mergesort() function is a modified merge sort with exponential search intended for sorting data with pre-existing order.

The () and heapsort() functions sort an array of nmemb objects, the initial member of which is pointed to by base. The size of each object is specified by size. mergesort() behaves similarly, but that size be greater than “sizeof(void *) / 2”.

The contents of the array base are sorted in ascending order according to a comparison function pointed to by compar, which requires two arguments pointing to the objects being compared.

The comparison function must return an int less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

The functions () and heapsort() are stable, that is, if two members compare as equal, their order in the sorted array is undefined. The function mergesort() is stable.

The () function is an implementation of C.A.R. Hoare's “quicksort” algorithm, a variant of partition-exchange sorting; in particular, see D.E. Knuth's Algorithm Q. qsort() takes O N lg N average time. This implementation uses median selection to avoid its O N**2 worst-case behavior and will fall back to heapsort() if the recursion depth exceeds 2 lg N.

The () function 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. This implementation of heapsort() is implemented without recursive function calls.

The function () requires additional memory of size nmemb * size bytes; it should be used only when space is not at a premium. mergesort() is optimized for data with pre-existing order; its worst case time is O N lg N; its best case is O N.

Normally, () is faster than mergesort(), which is faster than heapsort(). Memory availability and pre-existing order in the data can make this untrue.

The heapsort() and mergesort() functions return the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *array[] = { "XX", "YYY", "Z" };
#define N (sizeof(array) / sizeof(array[0]))

int
cmp(const void *a, const void *b)
{
	/*
	 * a and b point to elements of the array.
	 * Cast and dereference to obtain the actual elements,
	 * which are also pointers in this case.
	 */
	size_t lena = strlen(*(const char **)a);
	size_t lenb = strlen(*(const char **)b);
	/*
	 * Do not subtract the lengths. The difference between values
	 * cannot be represented by an int.
	 */
	return lena < lenb ? -1 : lena > lenb;
}

int
main()
{
	size_t i;

	qsort(array, N, sizeof(array[0]), cmp);
	for (i =  0; i < N; i++)
		printf("%s\n", array[i]);
}

It is almost always an error to use subtraction to compute the return value of the comparison function.

The heapsort() and mergesort() functions succeed unless:

[]
The size argument is zero, or the size argument to mergesort() is less than “sizeof(void *) / 2”.
[]
heapsort() or mergesort() were unable to allocate memory.

sort(1), radixsort(3)

Hoare, C.A.R., Quicksort, The Computer Journal, 5:1, pp. 10-15, 1962.

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, pp. 467-464, January 1993.

Bentley, J.L. and McIlroy, M.D., Engineering a Sort Function, Software - Practice and Experience, Vol. 23(11), pp. 1249-1265, November 1993.

Musser, D., Introspective Sorting and Selection Algorithms, Software - Practice and Experience, Vol. 27(8), pp. 983-993, August 1997.

Previous versions of qsort() did not permit the comparison routine itself to call qsort(). This is no longer true.

The qsort() function conforms to ANSI X3.159-1989 (“ANSI C89”).

A qsort() function first appeared in Version 2 AT&T UNIX.

February 8, 2020 OpenBSD-7.3