]>
Commit | Line | Data |
---|---|---|
a1eaacb1 | 1 | '\" t |
fea681da MK |
2 | .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) |
3 | .\" | |
5fbde956 | 4 | .\" SPDX-License-Identifier: Linux-man-pages-copyleft |
fea681da MK |
5 | .\" |
6 | .\" References consulted: | |
7 | .\" Linux libc source code | |
8 | .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) | |
9 | .\" 386BSD man pages | |
10 | .\" | |
11 | .\" Modified 1993-03-29, David Metcalfe | |
12 | .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu) | |
788be124 | 13 | .\" 2006-01-15, mtk, Added example program. |
6d388dc9 MK |
14 | .\" Modified 2012-03-08, Mark R. Bannister <cambridge@users.sourceforge.net> |
15 | .\" and Ben Bacarisse <software@bsb.me.uk> | |
16 | .\" Document qsort_r() | |
fea681da | 17 | .\" |
a5ebdc8d | 18 | .TH qsort 3 (date) "Linux man-pages (unreleased)" |
fea681da | 19 | .SH NAME |
b2db77f5 | 20 | qsort, qsort_r \- sort an array |
b27c428d AC |
21 | .SH LIBRARY |
22 | Standard C library | |
8fc3b2cf | 23 | .RI ( libc ", " \-lc ) |
fea681da MK |
24 | .SH SYNOPSIS |
25 | .nf | |
26 | .B #include <stdlib.h> | |
68e4db0a | 27 | .PP |
c64cd13e AC |
28 | .BI "void qsort(void " base [. size " * ." nmemb "], size_t " nmemb ", \ |
29 | size_t " size , | |
30 | .BI " int (*" compar ")(const void [." size "], \ | |
31 | const void [." size ])); | |
32 | .BI "void qsort_r(void " base [. size " * ." nmemb "], size_t " nmemb ", \ | |
33 | size_t " size , | |
34 | .BI " int (*" compar ")(const void [." size "], \ | |
35 | const void [." size "], void *)," | |
a094fe6d | 36 | .BI " void *" arg ");" |
fea681da | 37 | .fi |
68e4db0a | 38 | .PP |
d39ad78f | 39 | .RS -4 |
b2db77f5 BB |
40 | Feature Test Macro Requirements for glibc (see |
41 | .BR feature_test_macros (7)): | |
d39ad78f | 42 | .RE |
68e4db0a | 43 | .PP |
b2db77f5 | 44 | .BR qsort_r (): |
9d2adbae MK |
45 | .nf |
46 | _GNU_SOURCE | |
47 | .fi | |
fea681da | 48 | .SH DESCRIPTION |
60a90ecd MK |
49 | The |
50 | .BR qsort () | |
51 | function sorts an array with \fInmemb\fP elements of | |
c13182ef MK |
52 | size \fIsize\fP. |
53 | The \fIbase\fP argument points to the start of the | |
fea681da MK |
54 | array. |
55 | .PP | |
56 | The contents of the array are sorted in ascending order according to a | |
57 | comparison function pointed to by \fIcompar\fP, which is called with two | |
58 | arguments that point to the objects being compared. | |
59 | .PP | |
60 | The comparison function must return an integer less than, equal to, or | |
61 | greater than zero if the first argument is considered to be respectively | |
c13182ef | 62 | less than, equal to, or greater than the second. |
6d388dc9 MK |
63 | If two members compare as equal, |
64 | their order in the sorted array is undefined. | |
a094fe6d MB |
65 | .PP |
66 | The | |
67 | .BR qsort_r () | |
68 | function is identical to | |
69 | .BR qsort () | |
70 | except that the comparison function | |
71 | .I compar | |
6d388dc9 MK |
72 | takes a third argument. |
73 | A pointer is passed to the comparison function via | |
a094fe6d MB |
74 | .IR arg . |
75 | In this way, the comparison function does not need to use global variables to | |
6d388dc9 | 76 | pass through arbitrary arguments, and is therefore reentrant and safe to |
a094fe6d | 77 | use in threads. |
47297adb | 78 | .SH RETURN VALUE |
60a90ecd MK |
79 | The |
80 | .BR qsort () | |
a094fe6d MB |
81 | and |
82 | .BR qsort_r () | |
83 | functions return no value. | |
b24d9e5a ZL |
84 | .SH ATTRIBUTES |
85 | For an explanation of the terms used in this section, see | |
86 | .BR attributes (7). | |
87 | .TS | |
88 | allbox; | |
c466875e | 89 | lbx lb lb |
b24d9e5a ZL |
90 | l l l. |
91 | Interface Attribute Value | |
92 | T{ | |
9e54434e BR |
93 | .na |
94 | .nh | |
b24d9e5a ZL |
95 | .BR qsort (), |
96 | .BR qsort_r () | |
97 | T} Thread safety MT-Safe | |
98 | .TE | |
847e0d88 | 99 | .sp 1 |
3113c7f3 | 100 | .SH STANDARDS |
4131356c AC |
101 | .TP |
102 | .BR qsort () | |
103 | C11, POSIX.1-2008. | |
104 | .SH HISTORY | |
105 | .TP | |
106 | .BR qsort () | |
107 | POSIX.1-2001, C89, SVr4, 4.3BSD. | |
108 | .TP | |
109 | .BR qsort_r () | |
110 | glibc 2.8. | |
19c98696 | 111 | .SH NOTES |
c13182ef | 112 | To compare C strings, the comparison function can call |
fb186734 | 113 | .BR strcmp (3), |
cb478668 | 114 | as shown in the example below. |
a14af333 | 115 | .SH EXAMPLES |
788be124 MK |
116 | For one example of use, see the example under |
117 | .BR bsearch (3). | |
847e0d88 | 118 | .PP |
a057d61f | 119 | Another example is the following program, |
48a1cb49 | 120 | which sorts the strings given in its command-line arguments: |
bdd915e2 | 121 | .PP |
b0b6ab4e | 122 | .\" SRC BEGIN (qsort.c) |
bdd915e2 | 123 | .EX |
788be124 MK |
124 | #include <stdio.h> |
125 | #include <stdlib.h> | |
788be124 | 126 | #include <string.h> |
fe5dba13 | 127 | \& |
788be124 MK |
128 | static int |
129 | cmpstringp(const void *p1, const void *p2) | |
130 | { | |
7c5bd0eb | 131 | /* The actual arguments to this function are "pointers to |
9daa4fb9 | 132 | pointers to char", but strcmp(3) arguments are "pointers |
46b20ca1 | 133 | to char", hence the following cast plus dereference. */ |
fe5dba13 | 134 | \& |
9ba82f22 | 135 | return strcmp(*(const char **) p1, *(const char **) p2); |
788be124 | 136 | } |
fe5dba13 | 137 | \& |
788be124 MK |
138 | int |
139 | main(int argc, char *argv[]) | |
140 | { | |
6b34fb3f | 141 | if (argc < 2) { |
d1a71985 | 142 | fprintf(stderr, "Usage: %s <string>...\en", argv[0]); |
5a6194a4 | 143 | exit(EXIT_FAILURE); |
6b34fb3f | 144 | } |
fe5dba13 | 145 | \& |
29059a65 | 146 | qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp); |
fe5dba13 | 147 | \& |
b42296e4 | 148 | for (size_t j = 1; j < argc; j++) |
788be124 MK |
149 | puts(argv[j]); |
150 | exit(EXIT_SUCCESS); | |
151 | } | |
bdd915e2 | 152 | .EE |
b0b6ab4e | 153 | .\" SRC END |
47297adb | 154 | .SH SEE ALSO |
fea681da MK |
155 | .BR sort (1), |
156 | .BR alphasort (3), | |
157 | .BR strcmp (3), | |
158 | .BR versionsort (3) |