BSD 4_4 development
[unix-history] / usr / share / man / cat3 / mergesort.0
QSORT(3) BSD Programmer's Manual QSORT(3)
N\bNA\bAM\bME\bE
q\bqs\bso\bor\brt\bt,\b, h\bhe\bea\bap\bps\bso\bor\brt\bt,\b, m\bme\ber\brg\bge\bes\bso\bor\brt\bt - sort functions
S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS
#\b#i\bin\bnc\bcl\blu\bud\bde\be <\b<s\bst\btd\bdl\bli\bib\bb.\b.h\bh>\b>
_\bv_\bo_\bi_\bd
q\bqs\bso\bor\brt\bt(_\bv_\bo_\bi_\bd _\b*_\bb_\ba_\bs_\be, _\bs_\bi_\bz_\be_\b__\bt _\bn_\bm_\be_\bm_\bb, _\bs_\bi_\bz_\be_\b__\bt _\bs_\bi_\bz_\be,
_\bi_\bn_\bt _\b(_\b*_\bc_\bo_\bm_\bp_\ba_\br_\b)_\b(_\bc_\bo_\bn_\bs_\bt _\bv_\bo_\bi_\bd _\b*_\b, _\bc_\bo_\bn_\bs_\bt _\bv_\bo_\bi_\bd _\b*_\b));
_\bi_\bn_\bt
h\bhe\bea\bap\bps\bso\bor\brt\bt(_\bv_\bo_\bi_\bd _\b*_\bb_\ba_\bs_\be, _\bs_\bi_\bz_\be_\b__\bt _\bn_\bm_\be_\bm_\bb, _\bs_\bi_\bz_\be_\b__\bt _\bs_\bi_\bz_\be,
_\bi_\bn_\bt _\b(_\b*_\bc_\bo_\bm_\bp_\ba_\br_\b)_\b(_\bc_\bo_\bn_\bs_\bt _\bv_\bo_\bi_\bd _\b*_\b, _\bc_\bo_\bn_\bs_\bt _\bv_\bo_\bi_\bd _\b*_\b));
_\bi_\bn_\bt
m\bme\ber\brg\bge\bes\bso\bor\brt\bt(_\bv_\bo_\bi_\bd _\b*_\bb_\ba_\bs_\be, _\bs_\bi_\bz_\be_\b__\bt _\bn_\bm_\be_\bm_\bb, _\bs_\bi_\bz_\be_\b__\bt _\bs_\bi_\bz_\be,
_\bi_\bn_\bt _\b(_\b*_\bc_\bo_\bm_\bp_\ba_\br_\b)_\b(_\bc_\bo_\bn_\bs_\bt _\bv_\bo_\bi_\bd _\b*_\b, _\bc_\bo_\bn_\bs_\bt _\bv_\bo_\bi_\bd _\b*_\b));
D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN
The q\bqs\bso\bor\brt\bt() function is a modified partition-exchange sort, or quicksort.
The h\bhe\bea\bap\bps\bso\bor\brt\bt() function is a modified selection sort. The m\bme\ber\brg\bge\bes\bso\bor\brt\bt()
function is a modified merge sort with exponential search intended for
sorting data with pre-existing order.
The q\bqs\bso\bor\brt\bt() and h\bhe\bea\bap\bps\bso\bor\brt\bt() functions sort an array of _\bn_\bm_\be_\bm_\bb objects, the
initial member of which is pointed to by _\bb_\ba_\bs_\be. The size of each object is
specified by _\bs_\bi_\bz_\be. M\bMe\ber\brg\bge\bes\bso\bor\brt\bt() behaves similarly, but _\br_\be_\bq_\bu_\bi_\br_\be_\bs that _\bs_\bi_\bz_\be
be greater than ``sizeof(void *) / 2''.
The contents of the array _\bb_\ba_\bs_\be are sorted in ascending order according to
a comparison function pointed to by _\bc_\bo_\bm_\bp_\ba_\br, which requires two arguments
pointing to the objects being compared.
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.
The functions q\bqs\bso\bor\brt\bt() and h\bhe\bea\bap\bps\bso\bor\brt\bt() are _\bn_\bo_\bt stable, that is, if two mem-
bers compare as equal, their order in the sorted array is undefined. The
function m\bme\ber\brg\bge\bes\bso\bor\brt\bt() is stable.
The q\bqs\bso\bor\brt\bt() function is an implementation of C.A.R. Hoare's ``quicksort''
algorithm, a variant of partition-exchange sorting; in particular, see
D.E. Knuth's Algorithm Q. Q\bQs\bso\bor\brt\bt() takes O N lg N average time. This im-
plementation uses median selection to avoid its O N**2 worst-case behav-
ior.
The h\bhe\bea\bap\bps\bso\bor\brt\bt() function is an implementation of J.W.J. William's ``heap-
sort'' algorithm, a variant of selection sorting; in particular, see D.E.
Knuth's Algorithm H. H\bHe\bea\bap\bps\bso\bor\brt\bt() takes O N lg N worst-case time. Its
_\bo_\bn_\bl_\by advantage over q\bqs\bso\bor\brt\bt() is that it uses almost no additional memory;
while q\bqs\bso\bor\brt\bt() does not allocate memory, it is implemented using recur-
sion.
The function m\bme\ber\brg\bge\bes\bso\bor\brt\bt() requires additional memory of size _\bn_\bm_\be_\bm_\bb _\b* _\bs_\bi_\bz_\be
bytes; it should be used only when space is not at a premium.
M\bMe\ber\brg\bge\bes\bso\bor\brt\bt() is optimized for data with pre-existing order; its worst case
time is O N lg N; its best case is O N.
Normally, q\bqs\bso\bor\brt\bt() is faster than m\bme\ber\brg\bge\bes\bso\bor\brt\bt() is faster than h\bhe\bea\bap\bps\bso\bor\brt\bt().
Memory availability and pre-existing order in the data can make this un-
true.
R\bRE\bET\bTU\bUR\bRN\bN V\bVA\bAL\bLU\bUE\bES\bS
The q\bqs\bso\bor\brt\bt() function returns no value.
Upon successful completion, h\bhe\bea\bap\bps\bso\bor\brt\bt() and m\bme\ber\brg\bge\bes\bso\bor\brt\bt() return 0. Other-
wise, they return -1 and the global variable _\be_\br_\br_\bn_\bo is set to indicate the
error.
E\bER\bRR\bRO\bOR\bRS\bS
The h\bhe\bea\bap\bps\bso\bor\brt\bt() function succeeds unless:
[EINVAL] The _\bs_\bi_\bz_\be argument is zero, or, the _\bs_\bi_\bz_\be argument to
m\bme\ber\brg\bge\bes\bso\bor\brt\bt() is less than ``sizeof(void *) / 2''.
[ENOMEM] H\bHe\bea\bap\bps\bso\bor\brt\bt() or m\bme\ber\brg\bge\bes\bso\bor\brt\bt() were unable to allocate memory.
C\bCO\bOM\bMP\bPA\bAT\bTI\bIB\bBI\bIL\bLI\bIT\bTY\bY
Previous versions of q\bqs\bso\bor\brt\bt() did not permit the comparison routine itself
to call q\bqs\bso\bor\brt\bt(_\b3). This is no longer true.
S\bSE\bEE\bE A\bAL\bLS\bSO\bO
sort(1), radixsort(3)
Hoare, C.A.R., "Quicksort", _\bT_\bh_\be _\bC_\bo_\bm_\bp_\bu_\bt_\be_\br _\bJ_\bo_\bu_\br_\bn_\ba_\bl, 5:1, pp. 10-15, 1962.
Williams, J.W.J, "Heapsort", _\bC_\bo_\bm_\bm_\bu_\bn_\bi_\bc_\ba_\bt_\bi_\bo_\bn_\bs _\bo_\bf _\bt_\bh_\be _\bA_\bC_\bM, 7:1, pp. 347-348,
1964.
Knuth, D.E., "Sorting and Searching", _\bT_\bh_\be _\bA_\br_\bt _\bo_\bf _\bC_\bo_\bm_\bp_\bu_\bt_\be_\br _\bP_\br_\bo_\bg_\br_\ba_\bm_\bm_\bi_\bn_\bg,
Vol. 3, pp. 114-123, 145-149, 1968.
Mcilroy, P.M., "Optimistic Sorting and Information Theoretic Complexity",
_\bF_\bo_\bu_\br_\bt_\bh _\bA_\bn_\bn_\bu_\ba_\bl _\bA_\bC_\bM_\b-_\bS_\bI_\bA_\bM _\bS_\by_\bm_\bp_\bo_\bs_\bi_\bu_\bm _\bo_\bn _\bD_\bi_\bs_\bc_\br_\be_\bt_\be _\bA_\bl_\bg_\bo_\br_\bi_\bt_\bh_\bm_\bs, January 1992.
Bentley, J.L., "Engineering a Sort Function", _\bb_\be_\bn_\bt_\bl_\be_\by_\b@_\br_\be_\bs_\be_\ba_\br_\bc_\bh_\b._\ba_\bt_\bt_\b._\bc_\bo_\bm,
January 1992.
S\bST\bTA\bAN\bND\bDA\bAR\bRD\bDS\bS
The q\bqs\bso\bor\brt\bt() function conforms to ANSI C X3.159-1989 (``ANSI C '').
4.4BSD June 4, 1993 2