QSORT(3)                   POSIX Programmer's Manual                  QSORT(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       qsort -- sort a table of data

SSYYNNOOPPSSIISS
       #include <stdlib.h>

       void qsort(void *_b_a_s_e, size_t _n_e_l, size_t _w_i_d_t_h,
           int (*_c_o_m_p_a_r)(const void *, const void *));

DDEESSCCRRIIPPTTIIOONN
       The functionality described on this reference page is aligned with the
       ISO C standard. Any conflict between the requirements described here
       and the ISO C standard is unintentional. This volume of POSIX.1-2017
       defers to the ISO C standard.

       The _q_s_o_r_t() function shall sort an array of _n_e_l objects, the initial
       element of which is pointed to by _b_a_s_e.  The size of each object, in
       bytes, is specified by the _w_i_d_t_h argument. If the _n_e_l argument has the
       value zero, the comparison function pointed to by _c_o_m_p_a_r shall not be
       called and no rearrangement shall take place.

       The application shall ensure that the comparison function pointed to by
       _c_o_m_p_a_r does not alter the contents of the array. The implementation may
       reorder elements of the array between calls to the comparison function,
       but shall not alter the contents of any individual element.

       When the same objects (consisting of width bytes, irrespective of their
       current positions in the array) are passed more than once to the
       comparison function, the results shall be consistent with one another.
       That is, they shall define a total ordering on the array.

       The contents of the array shall be sorted in ascending order according
       to a comparison function. The _c_o_m_p_a_r argument is a pointer to the
       comparison function, which is called with two arguments that point to
       the elements being compared. The application shall ensure that the
       function returns an integer less than, equal to, or greater than 0, if
       the first argument is considered respectively less than, equal to, or
       greater than the second. If two members compare as equal, their order
       in the sorted array is unspecified.

RREETTUURRNN VVAALLUUEE
       The _q_s_o_r_t() function shall not return a value.

EERRRROORRSS
       No errors are defined.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
       None.

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The comparison function need not compare every byte, so arbitrary data
       may be contained in the elements in addition to the values being
       compared.

RRAATTIIOONNAALLEE
       The requirement that each argument (hereafter referred to as _p_) to the
       comparison function is a pointer to elements of the array implies that
       for every call, for each argument separately, all of the following
       expressions are non-zero:


           ((char *)p - (char *)base) % width == 0
           (char *)p >= (char *)base
           (char *)p < (char *)base + nel * width

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _a_l_p_h_a_s_o_r_t()

       The Base Definitions volume of POSIX.1-2017, <<ssttddlliibb..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2017                             QSORT(3)
