1QSORT(P) POSIX Programmer's Manual QSORT(P)
2
3
4
6 qsort - sort a table of data
7
9 #include <stdlib.h>
10
11 void qsort(void *base, size_t nel, size_t width,
12 int (*compar)(const void *, const void *));
13
14
16 The qsort() function shall sort an array of nel objects, the initial
17 element of which is pointed to by base. The size of each object, in
18 bytes, is specified by the width argument. If the nel argument has the
19 value zero, the comparison function pointed to by compar shall not be
20 called and no rearrangement shall take place.
21
22 The application shall ensure that the comparison function pointed to by
23 compar does not alter the contents of the array. The implementation
24 may reorder elements of the array between calls to the comparison func‐
25 tion, but shall not alter the contents of any individual element.
26
27 When the same objects (consisting of width bytes, irrespective of their
28 current positions in the array) are passed more than once to the com‐
29 parison function, the results shall be consistent with one another.
30 That is, they shall define a total ordering on the array.
31
32 The contents of the array shall be sorted in ascending order according
33 to a comparison function. The compar argument is a pointer to the com‐
34 parison function, which is called with two arguments that point to the
35 elements being compared. The application shall ensure that the function
36 returns an integer less than, equal to, or greater than 0, if the first
37 argument is considered respectively less than, equal to, or greater
38 than the second. If two members compare as equal, their order in the
39 sorted array is unspecified.
40
42 The qsort() function shall not return a value.
43
45 No errors are defined.
46
47 The following sections are informative.
48
50 None.
51
53 The comparison function need not compare every byte, so arbitrary data
54 may be contained in the elements in addition to the values being com‐
55 pared.
56
58 The requirement that each argument (hereafter referred to as p) to the
59 comparison function is a pointer to elements of the array implies that
60 for every call, for each argument separately, all of the following
61 expressions are nonzero:
62
63
64 ((char *)p - (char *)base) % width == 0
65 (char *)p >= (char *)base
66 (char *)p < (char *)base + nel * width
67
69 None.
70
72 The Base Definitions volume of IEEE Std 1003.1-2001, <stdlib.h>
73
75 Portions of this text are reprinted and reproduced in electronic form
76 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
77 -- Portable Operating System Interface (POSIX), The Open Group Base
78 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
79 Electrical and Electronics Engineers, Inc and The Open Group. In the
80 event of any discrepancy between this version and the original IEEE and
81 The Open Group Standard, the original IEEE and The Open Group Standard
82 is the referee document. The original Standard can be obtained online
83 at http://www.opengroup.org/unix/online.html .
84
85
86
87IEEE/The Open Group 2003 QSORT(P)