qqssoorrtt() -- General Function (libc)

Sort arrays in memory
#iinncclluuddee <ssttddlliibb.hh>
vvooiidd qqssoorrtt(_d_a_t_a, _n, _s_i_z_e, _c_o_m_p)
cchhaarr *_d_a_t_a; iinntt _n, _s_i_z_e; iinntt (*_c_o_m_p)();

qqssoorrtt() is  a generalized algorithm  for sorting arrays of  data in memory,
using  C. A.  R.  Hoare's ``quicksort''  algorithm.  qqssoorrtt()  works with  a
sequential array  of memory called _d_a_t_a,  which is divided into  _n parts of
_s_i_z_e bytes  each.  In  practice, _d_a_t_a  is usually an  array of  pointers or
structures, and _s_i_z_e is the  ssiizzeeooff the pointer or structure.  Each routine
compares pairs of items  and exchanges them as required.  The user-supplied
routine  to  which  _c_o_m_p points  performs  the  comparison.   It is  called
repeatedly, as follows:

    (*comp)(p1, p2)
    char *p1, *p2;

Here, _p_1 and _p_2 each point  to a block of _s_i_z_e bytes in the _d_a_t_a array.  In
practice, they are usually  pointers to pointers or pointers to structures.
The comparison  routine must return  a negative, zero,  or positive result,
depending on whether  _p_1 is logically less than, equal  to, or greater than
_p_2, respectively.

_E_x_a_m_p_l_e
For an example of this function, see the entry for mmaalllloocc().

_S_e_e _A_l_s_o
lliibbcc, sshheellllssoorrtt(), ssttrrccmmpp(), ssttddlliibb.hh, ssttrrnnccmmpp()
_T_h_e _A_r_t _o_f _C_o_m_p_u_t_e_r _P_r_o_g_r_a_m_m_i_n_g, vol. 3
ANSI Standard, section 7.10.5.2
POSIX Standard, section 8.1

_N_o_t_e_s
The COHERENT library  also includes the sorting function sshheellllssoorrtt(). These
functions use  different algorithms for  sorting items; each  algorithm has
its  strengths and  weaknesses.   In general,  the  quicksort algorithm  is
faster than the shellsort algorithm for large arrays, whereas the shellsort
algorithm  is  faster for  small  arrays  (say, 50  items  or fewer).   The
quicksort algorithm  also performs poorly on arrays with  a small number of
keys, e.g., an array of 1,000 items whose keys are all `7' and `8'.

To get around these limitations, the COHERENT implementation of qqssoorrtt() has
an  adaptive algorithm  that  recognizes when  the  quicksort algorithm  is
performing badly, and calls sshheellllssoorrtt() in its place.
