sort functions

You're viewing an older version of this page (#3272). View the current version.

View versions (4)

NAME

<span class="Nm" id="qsort">qsort</span>, <span class="Nm" id="qsort_r">qsort_r</span>, <span class="Nm" id="heapsort">heapsort</span>, <span class="Nm" id="mergesort">mergesort</span>

INTERFACE

#include <stdlib.h> void &#92;n qsort( void *base, size_t nmemb, size_t size, int (*compar ) (const void *, const void * ),) &#92;n

void &#92;n qsort_r( void *base, size_t nmemb, size_t size, void *thunk, int (*compar ) (void *, const void *, const void * ),) &#92;n

int &#92;n heapsort( void *base, size_t nmemb, size_t size, int (*compar ) (const void *, const void * ),) &#92;n

int &#92;n mergesort( void *base, size_t nmemb, size_t size, int (*compar ) (const void *, const void * ),) &#92;n

DESCRIPTION

The qsort function is a modified partition-exchange sort, or quicksort. The heapsort function is a modified selection sort. The mergesort function is a modified merge sort with exponential search intended for sorting data with pre-existing order. &#92;n &#92;n The qsort and heapsort functions sort an array of &#92;c nmemb objects, the initial member of which is pointed to by &#92;c base. The size of each object is specified by &#92;c size. The mergesort function behaves similarly, but <span class="Em">requires</span> that &#92;c size be greater than "<span class="Dq">sizeof(void *) / 2</span>". &#92;n &#92;n The contents of the array &#92;c base are sorted in ascending order according to a comparison function pointed to by &#92;c compar, which requires two arguments pointing to the objects being compared. &#92;n &#92;n 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. &#92;n &#92;n The qsort_r function behaves identically to qsort , except that it takes an additional argument, &#92;c thunk, which is passed unchanged as the first argument to function pointed to &#92;c compar. This allows the comparison function to access additional data without using global variables, and thus qsort_r is suitable for use in functions which must be reentrant. &#92;n &#92;n The algorithms implemented by qsort , qsort_r , and heapsort are <span class="Em">not</span> stable, that is, if two members compare as equal, their order in the sorted array is undefined. The mergesort algorithm is stable. &#92;n &#92;n The qsort and qsort_r functions are an implementation of C.A.R. Hoare's "<span class="Dq">quicksort</span>" algorithm, a variant of partition-exchange sorting; in particular, see <span class="An">D.E.</span> <span class="An">Knuth</span>'s "<span class="refT">Algorithm Q</span>". <span class="Sy">Quicksort</span> takes O N lg N average time. This implementation uses median selection to avoid its O N**2 worst-case behavior. &#92;n &#92;n The heapsort function is an implementation of <span class="An">J.W.J. William</span>'s "<span class="Dq">heapsort</span>" algorithm, a variant of selection sorting; in particular, see <span class="An">D.E. Knuth</span>'s "<span class="refT">Algorithm H</span>". <span class="Sy">Heapsort</span> takes O N lg N worst-case time. Its <span class="Em">only</span> advantage over qsort is that it uses almost no additional memory; while qsort does not allocate memory, it is implemented using recursion. &#92;n &#92;n The function mergesort requires additional memory of size &#92;c nmemb &#92;c * &#92;c size bytes; it should be used only when space is not at a premium. The mergesort function is optimized for data with pre-existing order; its worst case time is O N lg N; its best case is O N. &#92;n &#92;n Normally, qsort is faster than mergesort is faster than heapsort . Memory availability and pre-existing order in the data can make this untrue.

RETURN VALUES

The qsort and qsort_r functions return no value. &#92;n &#92;n <span class="Rv">-std</span> <span class="Rv">heapsort</span> <span class="Rv">mergesort</span>

ERRORS

The heapsort and mergesort functions succeed unless: <table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EINVAL</span></span>] </td> <td valign="top"> The &#92;c size argument is zero, or, the &#92;c size argument to mergesort is less than "<span class="Dq">sizeof(void *) / 2</span>". </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">ENOMEM</span></span>] </td> <td valign="top"> The heapsort or mergesort functions were unable to allocate memory. </td> </tr> </table>

COMPATIBILITY

Previous versions of qsort did not permit the comparison routine itself to call qsort ( <span class="Fn">3</span> ). This is no longer true.

SEE ALSO

sort (1) , reference:radixsort (3) &#92;n &#92;n <span class="refA">Hoare,</span> <span class="refA">C.A.R.</span> (<span class="refD">1962</span>) "<span class="refT">Quicksort</span>" <span class="refJ">The Computer Journal</span> <span class="refV">5:1</span> <span class="refP">pp.</span> <span class="refP">10-15</span> &#92;n &#92;n <span class="refA">Williams,</span> <span class="refA">J.W.J</span> (<span class="refD">1964</span>) "<span class="refT">Heapsort</span>" <span class="refJ">Communications of the ACM</span> <span class="refV">7:1</span> <span class="refP">pp.</span> <span class="refP">347-348</span> &#92;n &#92;n <span class="refA">Knuth,</span> <span class="refA">D.E.</span> (<span class="refD">1968</span>) <span class="refB">The Art of Computer Programming</span> <span class="refV">Vol.</span> <span class="refV">3</span> "<span class="refT">Sorting and Searching</span>" <span class="refP">pp.</span> <span class="refP">114-123,</span> <span class="refP">145-149</span> &#92;n &#92;n <span class="refA">McIlroy,</span> <span class="refA">P.M.</span> "<span class="refT">Optimistic Sorting and Information Theoretic Complexity</span>" <span class="refJ">Fourth Annual ACM-SIAM Symposium on Discrete Algorithms</span> <span class="refV">January</span> <span class="refV">1992</span> &#92;n &#92;n <span class="refA">Bentley,</span> <span class="refA">J.L.</span> <span class="refA">McIlroy,</span> <span class="refA">M.D.</span> "<span class="refT">Engineering a Sort Function</span>" <span class="refJ">Software--Practice and Experience</span> <span class="refV">Vol.</span> <span class="refV">23(11)</span> <span class="refP">pp.</span> <span class="refP">1249-1265</span> (<span class="refD">November 1993</span>)

STANDARDS

The qsort function conforms to