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