Changes: Ready for 5.13
[man-pages.git] / man3 / qsort.3
blobe020da36be9f9a1a3f805103c5d0bf6611d305a8
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\"     Linux libc source code
27 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\"     386BSD man pages
29 .\"
30 .\" Modified 1993-03-29, David Metcalfe
31 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
32 .\" 2006-01-15, mtk, Added example program.
33 .\" Modified 2012-03-08, Mark R. Bannister <cambridge@users.sourceforge.net>
34 .\"                  and Ben Bacarisse <software@bsb.me.uk>
35 .\"     Document qsort_r()
36 .\"
37 .TH QSORT 3 2021-03-22 "" "Linux Programmer's Manual"
38 .SH NAME
39 qsort, qsort_r \- sort an array
40 .SH SYNOPSIS
41 .nf
42 .B #include <stdlib.h>
43 .PP
44 .BI "void qsort(void *" base ", size_t " nmemb ", size_t " size ,
45 .BI "           int (*" compar ")(const void *, const void *));"
46 .BI "void qsort_r(void *" base ", size_t " nmemb ", size_t " size ,
47 .BI "           int (*" compar ")(const void *, const void *, void *),"
48 .BI "           void *" arg ");"
49 .fi
50 .PP
51 .RS -4
52 Feature Test Macro Requirements for glibc (see
53 .BR feature_test_macros (7)):
54 .RE
55 .PP
56 .BR qsort_r ():
57 .nf
58     _GNU_SOURCE
59 .fi
60 .SH DESCRIPTION
61 The
62 .BR qsort ()
63 function sorts an array with \fInmemb\fP elements of
64 size \fIsize\fP.
65 The \fIbase\fP argument points to the start of the
66 array.
67 .PP
68 The contents of the array are sorted in ascending order according to a
69 comparison function pointed to by \fIcompar\fP, which is called with two
70 arguments that point to the objects being compared.
71 .PP
72 The comparison function must return an integer less than, equal to, or
73 greater than zero if the first argument is considered to be respectively
74 less than, equal to, or greater than the second.
75 If two members compare as equal,
76 their order in the sorted array is undefined.
77 .PP
78 The
79 .BR qsort_r ()
80 function is identical to
81 .BR qsort ()
82 except that the comparison function
83 .I compar
84 takes a third argument.
85 A pointer is passed to the comparison function via
86 .IR arg .
87 In this way, the comparison function does not need to use global variables to
88 pass through arbitrary arguments, and is therefore reentrant and safe to
89 use in threads.
90 .SH RETURN VALUE
91 The
92 .BR qsort ()
93 and
94 .BR qsort_r ()
95 functions return no value.
96 .SH VERSIONS
97 .BR qsort_r ()
98 was added to glibc in version 2.8.
99 .SH ATTRIBUTES
100 For an explanation of the terms used in this section, see
101 .BR attributes (7).
102 .ad l
105 allbox;
106 lbx lb lb
107 l l l.
108 Interface       Attribute       Value
110 .BR qsort (),
111 .BR qsort_r ()
112 T}      Thread safety   MT-Safe
116 .sp 1
117 .SH CONFORMING TO
118 .BR qsort ():
119 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
120 .SH NOTES
121 To compare C strings, the comparison function can call
122 .BR strcmp (3),
123 as shown in the example below.
124 .SH EXAMPLES
125 For one example of use, see the example under
126 .BR bsearch (3).
128 Another example is the following program,
129 which sorts the strings given in its command-line arguments:
132 #include <stdio.h>
133 #include <stdlib.h>
134 #include <string.h>
136 static int
137 cmpstringp(const void *p1, const void *p2)
139     /* The actual arguments to this function are "pointers to
140        pointers to char", but strcmp(3) arguments are "pointers
141        to char", hence the following cast plus dereference. */
143     return strcmp(*(const char **) p1, *(const char **) p2);
147 main(int argc, char *argv[])
149     if (argc < 2) {
150         fprintf(stderr, "Usage: %s <string>...\en", argv[0]);
151         exit(EXIT_FAILURE);
152     }
154     qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
156     for (int j = 1; j < argc; j++)
157         puts(argv[j]);
158     exit(EXIT_SUCCESS);
161 .SH SEE ALSO
162 .BR sort (1),
163 .BR alphasort (3),
164 .BR strcmp (3),
165 .BR versionsort (3)