1QSORT(3P) POSIX Programmer's Manual QSORT(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 qsort — sort a table of data
13
15 #include <stdlib.h>
16
17 void qsort(void *base, size_t nel, size_t width,
18 int (*compar)(const void *, const void *));
19
21 The functionality described on this reference page is aligned with the
22 ISO C standard. Any conflict between the requirements described here
23 and the ISO C standard is unintentional. This volume of POSIX.1‐2017
24 defers to the ISO C standard.
25
26 The qsort() function shall sort an array of nel objects, the initial
27 element of which is pointed to by base. The size of each object, in
28 bytes, is specified by the width argument. If the nel argument has the
29 value zero, the comparison function pointed to by compar shall not be
30 called and no rearrangement shall take place.
31
32 The application shall ensure that the comparison function pointed to by
33 compar does not alter the contents of the array. The implementation may
34 reorder elements of the array between calls to the comparison function,
35 but shall not alter the contents of any individual element.
36
37 When the same objects (consisting of width bytes, irrespective of their
38 current positions in the array) are passed more than once to the com‐
39 parison function, the results shall be consistent with one another.
40 That is, they shall define a total ordering on the array.
41
42 The contents of the array shall be sorted in ascending order according
43 to a comparison function. The compar argument is a pointer to the com‐
44 parison function, which is called with two arguments that point to the
45 elements being compared. The application shall ensure that the function
46 returns an integer less than, equal to, or greater than 0, if the first
47 argument is considered respectively less than, equal to, or greater
48 than the second. If two members compare as equal, their order in the
49 sorted array is unspecified.
50
52 The qsort() function shall not return a value.
53
55 No errors are defined.
56
57 The following sections are informative.
58
60 None.
61
63 The comparison function need not compare every byte, so arbitrary data
64 may be contained in the elements in addition to the values being com‐
65 pared.
66
68 The requirement that each argument (hereafter referred to as p) to the
69 comparison function is a pointer to elements of the array implies that
70 for every call, for each argument separately, all of the following
71 expressions are non-zero:
72
73
74 ((char *)p - (char *)base) % width == 0
75 (char *)p >= (char *)base
76 (char *)p < (char *)base + nel * width
77
79 None.
80
82 alphasort()
83
84 The Base Definitions volume of POSIX.1‐2017, <stdlib.h>
85
87 Portions of this text are reprinted and reproduced in electronic form
88 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
89 table Operating System Interface (POSIX), The Open Group Base Specifi‐
90 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
91 Electrical and Electronics Engineers, Inc and The Open Group. In the
92 event of any discrepancy between this version and the original IEEE and
93 The Open Group Standard, the original IEEE and The Open Group Standard
94 is the referee document. The original Standard can be obtained online
95 at http://www.opengroup.org/unix/online.html .
96
97 Any typographical or formatting errors that appear in this page are
98 most likely to have been introduced during the conversion of the source
99 files to man page format. To report such errors, see https://www.ker‐
100 nel.org/doc/man-pages/reporting_bugs.html .
101
102
103
104IEEE/The Open Group 2017 QSORT(3P)