1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\" Modified 1993-03-29, David Metcalfe
29 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
30 .\" 2006-01-15, mtk, Added example program.
32 .\" FIXME glibc 2.8 added qsort_r(), which needs to be documented.
34 .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi
35 .\" all rights reserved.
36 .\" Translated 1997-01-21, YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
37 .\" Updated & Modified 2004-06-06, Yuichi SATO <ysato444@yahoo.co.jp>
38 .\" Updated 2006-01-18, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
40 .TH QSORT 3 2009-09-15 "" "Linux Programmer's Manual"
43 .\"O qsort \- sorts an array
44 qsort \- ÇÛÎó¤òʤÙÊѤ¨¤ë
48 .B #include <stdlib.h>
50 .BI "void qsort(void *" base ", size_t " nmemb ", size_t " size ,
51 .BI " int(*" compar ")(const void *, const void *));"
57 .\"O function sorts an array with \fInmemb\fP elements of
59 .\"O The \fIbase\fP argument points to the start of the
63 \fInmemb\fP ¸Ä¤ÎÂ礤µ \fIsize\fP ¤ÎÍ×ÁǤò¤â¤ÄÇÛÎó¤òʤÙÊѤ¨¤ë¡£
64 \fIbase\fP °ú¤¿ô¤ÏÇÛÎó¤ÎÀèƬ¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
66 .\"O The contents of the array are sorted in ascending order according to a
67 .\"O comparison function pointed to by \fIcompar\fP, which is called with two
68 .\"O arguments that point to the objects being compared.
69 \fIcompar\fP ¤ò¥Ý¥¤¥ó¥¿¤È¤¹¤ëÈæ³Ó´Ø¿ô¤Ë¤è¤Ã¤Æ¡¢
70 ÇÛÎó¤ÎÃæ¿È¤Ï¾º½ç (ÃͤÎÂ礤¤¤â¤Î¤Û¤É¸å¤ËʤֽçÈÖ) ¤Ëʤ٤é¤ì¤ë¡£
71 Èæ³Ó´Ø¿ô¤Î°ú¤¿ô¤ÏÈæ³Ó¤µ¤ì¤ë¤Õ¤¿¤Ä¤Î¥ª¥Ö¥¸¥§¥¯¥È¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
73 .\"O The comparison function must return an integer less than, equal to, or
74 .\"O greater than zero if the first argument is considered to be respectively
75 .\"O less than, equal to, or greater than the second.
76 .\"O If two members compare
77 .\"O as equal, their order in the sorted array is undefined.
78 Èæ³Ó´Ø¿ô¤Ï¡¢Âè°ì°ú¤¿ô¤¬ÂèÆó°ú¤¿ô¤ËÂФ·¤Æ¡¢
79 1) ¾®¤µ¤¤¡¢2) Åù¤·¤¤¡¢3) Â礤¤¤Î¤½¤ì¤¾¤ì¤Ë±þ¤¸¤Æ¡¢
80 1) ¥¼¥í¤è¤ê¾®¤µ¤¤À°¿ô¡¢2) ¥¼¥í¡¢3) ¥¼¥í¤è¤êÂ礤¤À°¿ô¤Î
81 ¤¤¤º¤ì¤«¤òÊÖ¤µ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
82 Æó¤Ä¤ÎÍ×ÁǤÎÈæ³Ó·ë²Ì¤¬Åù¤·¤¤¤È¤¡¢
83 ʤÙÊѤ¨¤¿¸å¤ÎÇÛÎó¤Ç¤Ï¡¢¤³¤ì¤éÆó¤Ä¤Î½ç½ø¤Ïµ¬Äꤵ¤ì¤Æ¤¤¤Ê¤¤¡£
84 .\"O .SH "RETURN VALUE"
88 .\"O function returns no value.
91 .\"O .SH "CONFORMING TO"
93 SVr4, 4.3BSD, C89, C99.
96 .\"O Library routines suitable for use as the
99 .\"O .BR alphasort (3)
101 .\"O .BR versionsort (3).
103 °ú¤¿ô¤Ë»ÈÍѤ¹¤ë¤Î¤ËŬ¤·¤Æ¤¤¤ë¥é¥¤¥Ö¥é¥ê¥ë¡¼¥Á¥ó¤È¤·¤Æ¤Ï
107 .\"O To compare C strings, the comparison function can call
109 .\"O as shown in the example below.
110 C ¤Îʸ»úÎó¤òÈæ³Ó¤¹¤ë¾ì¹ç¡¢°Ê²¼¤ÎÎã¤Ë¤¢¤ë¤è¤¦¤ËÈæ³Ó´Ø¿ô¤Ç
112 ¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤â¤Ç¤¤ë¡£
115 .\"O For one example of use, see the example under
116 .\"O .BR bsearch (3).
119 ¤Ë¤¢¤ëÎã¤ò»²¾È¤¹¤ë¤³¤È¡£
121 .\"O Another example is the following program,
122 .\"O which sorts the strings given in its command-line arguments:
123 °Ê²¼¤Î¥×¥í¥°¥é¥à¤ËÊ̤λÈÍÑÎã¤ò¼¨¤¹¡£¤³¤Î¥×¥í¥°¥é¥à¤Ï¡¢
124 ¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤¿ô¤Ç»ØÄꤵ¤ì¤¿Ê¸»úÎó¤ÎʤӴ¹¤¨¤ò¹Ô¤¦¡£
132 cmpstringp(const void *p1, const void *p2)
134 .\"O /* The actual arguments to this function are "pointers to
135 .\"O pointers to char", but strcmp(3) arguments are "pointers
136 .\"O to char", hence the following cast plus dereference */
137 /* ¤³¤Î´Ø¿ô¤Î¼ÂºÝ¤Î°ú¤¿ô¤Ï "char ·¿¤Ø¤Î¥Ý¥¤¥ó¥¿¤Î¥Ý¥¤¥ó¥¿" ¤À¤¬¡¢
138 strcmp(3) ¤Î°ú¤¿ô¤Ï "char ·¿¤Ø¤Î¥Ý¥¤¥ó¥¿" ¤Ç¤¢¤ë¡£
139 ¤½¤³¤Ç¡¢°Ê²¼¤Î¤è¤¦¤Ë¥¥ã¥¹¥È¤ò¤·¤Æ¤«¤é¥Ý¥¤¥ó¥¿¤ÎµÕ»²¾È¤ò¹Ô¤¦¡£*/
141 return strcmp(* (char * const *) p1, * (char * const *) p2);
145 main(int argc, char *argv[])
150 fprintf(stderr, "Usage: %s <string>...\\n", argv[0]);
154 qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
156 for (j = 1; j < argc; j++)