218 lines
6.2 KiB
HTML
218 lines
6.2 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of QSORT</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>QSORT</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
qsort, qsort_r - sort an array
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>></B>
|
|
|
|
<B>void qsort(void *</B><I>base</I><B>, size_t </B><I>nmemb</I><B>, size_t </B><I>size</I><B>,</B>
|
|
<B> int (*</B><I>compar</I><B>)(const void *, const void *));</B>
|
|
|
|
<B>void qsort_r(void *</B><I>base</I><B>, size_t </B><I>nmemb</I><B>, size_t </B><I>size</I><B>,</B>
|
|
<B> int (*</B><I>compar</I><B>)(const void *, const void *, void *),</B>
|
|
<B> void *</B><I>arg</I><B>);</B>
|
|
</PRE>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
|
|
<B>qsort_r</B>():
|
|
|
|
_GNU_SOURCE
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>qsort</B>()
|
|
|
|
function sorts an array with <I>nmemb</I> elements of
|
|
size <I>size</I>.
|
|
The <I>base</I> argument points to the start of the
|
|
array.
|
|
<P>
|
|
|
|
The contents of the array are sorted in ascending order according to a
|
|
comparison function pointed to by <I>compar</I>, which is called with two
|
|
arguments that point to the objects being compared.
|
|
<P>
|
|
|
|
The comparison function must return an integer less than, equal to, or
|
|
greater than zero if the first argument is considered to be respectively
|
|
less than, equal to, or greater than the second.
|
|
If two members compare as equal,
|
|
their order in the sorted array is undefined.
|
|
<P>
|
|
|
|
The
|
|
<B>qsort_r</B>()
|
|
|
|
function is identical to
|
|
<B>qsort</B>()
|
|
|
|
except that the comparison function
|
|
<I>compar</I>
|
|
|
|
takes a third argument.
|
|
A pointer is passed to the comparison function via
|
|
<I>arg</I>.
|
|
|
|
In this way, the comparison function does not need to use global variables to
|
|
pass through arbitrary arguments, and is therefore reentrant and safe to
|
|
use in threads.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
The
|
|
<B>qsort</B>()
|
|
|
|
and
|
|
<B>qsort_r</B>()
|
|
|
|
functions return no value.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>qsort_r</B>()
|
|
|
|
was added to glibc in version 2.8.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>qsort</B>(),
|
|
|
|
<B>qsort_r</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<P>
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
<B>qsort</B>():
|
|
|
|
POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
To compare C strings, the comparison function can call
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strcmp">strcmp</A></B>(3),
|
|
|
|
as shown in the example below.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
For one example of use, see the example under
|
|
<B><A HREF="/cgi-bin/man/man2html?3+bsearch">bsearch</A></B>(3).
|
|
|
|
<P>
|
|
|
|
Another example is the following program,
|
|
which sorts the strings given in its command-line arguments:
|
|
<P>
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
<P>
|
|
static int
|
|
cmpstringp(const void *p1, const void *p2)
|
|
{
|
|
<BR> /* The actual arguments to this function are "pointers to
|
|
<BR> pointers to char", but <A HREF="/cgi-bin/man/man2html?3+strcmp">strcmp</A>(3) arguments are "pointers
|
|
<BR> to char", hence the following cast plus dereference */
|
|
<P>
|
|
<BR> return strcmp(* (char * const *) p1, * (char * const *) p2);
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> int j;
|
|
<P>
|
|
<BR> if (argc < 2) {
|
|
<BR> fprintf(stderr, "Usage: %s <string>...\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
|
|
<P>
|
|
<BR> for (j = 1; j < argc; j++)
|
|
<BR> puts(argv[j]);
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+sort">sort</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+alphasort">alphasort</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strcmp">strcmp</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+versionsort">versionsort</A></B>(3)
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="5"><A HREF="#lbAF">VERSIONS</A><DD>
|
|
<DT id="6"><A HREF="#lbAG">ATTRIBUTES</A><DD>
|
|
<DT id="7"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="8"><A HREF="#lbAI">NOTES</A><DD>
|
|
<DT id="9"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DT id="10"><A HREF="#lbAK">SEE ALSO</A><DD>
|
|
<DT id="11"><A HREF="#lbAL">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:53 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|