#include <stdlib.h>
int mergesort(void *base, size_t nmemb,
size_t size, int (*compar)(const void *,
const void *));
The mergesort() function
sorts an array of nmemb objects, the initial element of
which is pointed to by base. The size of each object in
bytes is specified by size. The contents of the array are
sorted in ascending order according to a comparison function. The
compar argument is a pointer to the comparison function,
which is called with two arguments that point to the elements
being compared. The function must return an integer less than,
equal to, or greater than 0, if the first argument is considered
respectively less than, equal to, or greater than the second. If
two members compare as equal, their order in the sorted array is
unspecified.
The mergesort() function requires additional memory of size
nmemb * size bytes; it should only be used when
space is not at a premium. The algorithm is optimized for data with
pre-existing order; its worst case performance is O(n
log n); its average performance is O(n).
The mergesort() algorithm is a stable sort.
- base
-
Points to the memory to be sorted.
- nmemb
-
Is the number of objects in memory.
- size
-
Is the size of each object in memory.
- compar
-
Is the sorting function.
On success, heapsort() returns 0. On error, it returns -1
and sets errno to one of the following values:
- EINVAL
-
The size parameter is
less than (sizeof(void *) / 2).
- ENOMEM
-
Insufficient memory is available
to implement the sort algorithm.
4.4BSD.
MT-Safe.
None.
MKS Toolkit for Professional Developers
MKS Toolkit for Enterprise Developers
MKS Toolkit for Enterprise Developers 64-Bit Edition
- Functions:
- heapsort(), qsort(), radixsort(), sradixsort()
MKS Toolkit 9.2 Documentation Build 16.