File: qsort.3

package info (click to toggle)
manpages-ja 0.5.0.0.20080615-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 20,508 kB
  • ctags: 1
  • sloc: sh: 13,690; perl: 157; makefile: 114
file content (121 lines) | stat: -rw-r--r-- 3,799 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" References consulted:
.\"     Linux libc source code
.\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
.\"     386BSD man pages
.\"
.\" Modified 1993-03-29, David Metcalfe
.\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
.\" 2006-01-15, mtk, Added example program.
.\"
.\" Japanese Version Copyright (c) 1997 YOSHINO Takashi
.\"       all rights reserved.
.\" Translated 1997-01-21, YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
.\" Updated & Modified 2004-06-06, Yuichi SATO <ysato444@yahoo.co.jp>
.\" Updated 2006-01-18, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
.\"
.TH QSORT 3 2003-11-15 "" "Linux Programmer's Manual"
.SH ̾
qsort \- ¤Ѥ
.SH 
.nf
.B #include <stdlib.h>
.sp
.BI "void qsort(void *" base ", size_t " nmemb ", size_t " size ,
.BI "           int(*" compar ")(const void *, const void *));"
.fi
.SH 
.BR qsort ()
ؿϡ
\fInmemb\fP Ĥ礭 \fIsize\fP Ǥ¤Ѥ롣
\fIbase\fP ƬؤΥݥ󥿤Ǥ롣
.PP
\fIcompar\fP ݥ󥿤ȤӴؿˤäơ
ȤϾ (ͤ礭Τۤɸ¤ֽ) ¤٤롣
ӴؿΰӤդĤΥ֥ȤΥݥ󥿤Ǥ롣
.PP
ӴؿϡФơ
1) 2) 3) 礭Τ줾˱ơ
1) 꾮2) 3) 礭
줫֤ʤФʤʤ
ĤǤӷ̤Ȥ
¤ѤǤϡĤνϵꤵƤʤ
.SH ֤
.BR qsort ()
֤ͤʤ
.SH 
SVr4, 4.3BSD, C89, C99.
.SH 
.I compar
˻ѤΤŬƤ饤֥롼ȤƤ
.BR alphasort (3),
.BR versionsort (3)
C ʸӤ硢ʲˤ褦Ӵؿ
.BR strcmp (3)
ƤӽФȤǤ롣
.SH 
ˤĤƤϡ
.BR bsearch (3)
ˤ򻲾Ȥ뤳ȡ

ʲΥץ̤λ򼨤Υץϡ
ޥɥ饤ǻꤵ줿ʸ¤ӴԤ
.sp
.nf
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

static int
cmpstringp(const void *p1, const void *p2)
{
    /* δؿμºݤΰ "char ؤΥݥ󥿤Υݥ" 
       strcmp(3) ΰ "char ؤΥݥ" Ǥ롣
       ǡʲΤ褦˥㥹Ȥ򤷤Ƥݥ󥿤εջȤԤ*/

    return strcmp(* (char * const *) p1, * (char * const *) p2);
}

int
main(int argc, char *argv[])
{
    int j;

    assert(argc > 1);

    qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);

    for (j = 1; j < argc; j++)
        puts(argv[j]);
    exit(EXIT_SUCCESS);
}
.fi
.SH Ϣ
.BR sort (1),
.BR alphasort (3),
.BR strcmp (3),
.BR versionsort (3)