

qsort()                  General Function                 qsort()




Sort arrays in memory

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)();

qsort is  a generalized algorithm  for sorting arrays  of data in
memory, using  C. A.  R. Hoare's ``quicksort''  algorithm.  qsort
works with  a sequential  array of  memory called data,  which is
divided into  n parts of  size bytes each.  In  practice, data is
usually  an array  of  pointers or  structures, and  size is  the
sizeof the pointer  or structure.  Each routine compares pairs of
items and exchanges  them as required.  The user-supplied routine
to  which comp  points  performs the  comparison.   It is  called
repeatedly, as follows:


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


Here, p1 and  p2 each point to a block  of size bytes in the data
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  p1 is
logically less than, equal to, or greater than p2, respectively.

***** Example *****

For an example of this function, see the entry for malloc.

***** See Also *****

general functions, shellsort(), strcmp(), strncmp()
_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

***** Notes *****

qsort differs from the other sorting function, shellsort, in that
it uses a recursive algorithm that makes heavy use of the stack.


















COHERENT Lexicon                                           Page 1


