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