1QSORT(3) 386BSD Programmer's Manual QSORT(3) 2 3NNAAMMEE 4 qqssoorrtt,, hheeaappssoorrtt - sort functions 5 6SSYYNNOOPPSSIISS 7 ##iinncclluuddee <<ssttddlliibb..hh>> 8 9 _v_o_i_d 10 qqssoorrtt(_v_o_i_d *_b_a_s_e, _s_i_z_e__t _n_m_e_m_b, _s_i_z_e__t _s_i_z_e, 11 _i_n_t (*_c_o_m_p_a_r)(_c_o_n_s_t _v_o_i_d *, _c_o_n_s_t _v_o_i_d *)) 12 13 _i_n_t 14 hheeaappssoorrtt(_v_o_i_d *_b_a_s_e, _s_i_z_e__t _n_m_e_m_b, _s_i_z_e__t _s_i_z_e, 15 _i_n_t (*_c_o_m_p_a_r)(_c_o_n_s_t _v_o_i_d *, _c_o_n_s_t _v_o_i_d *)) 16 17DDEESSCCRRIIPPTTIIOONN 18 The qqssoorrtt() function is a modified partition-exchange sort, or quicksort. 19 The hheeaappssoorrtt() function is a modified selection sort. 20 21 The qqssoorrtt() and hheeaappssoorrtt() functions sort an array of _n_m_e_m_b objects, the 22 initial member of which is pointed to by _b_a_s_e. The size of each object is 23 specified by _s_i_z_e. 24 25 The contents of the array are sorted in ascending order according to a 26 comparison function pointed to by _c_o_m_p_a_r, which is called with two 27 arguments that point to the objects being compared. 28 29 The comparison function must return an integer less than, equal to, or 30 greater than zero if the first argument is considered to be respectively 31 less than, equal to, or greater than the second. 32 33 The functions qqssoorrtt() and hheeaappssoorrtt() are _n_o_t stable, that is, if two 34 members compare as equal, their order in the sorted array is undefined. 35 36 The qqssoorrtt() function is an implementation of C.A.R. Hoare's ``quicksort'' 37 algorithm, a variant of partition-exchange sorting; in particular, see 38 D.E. Knuth's Algorithm Q. QQssoorrtt() takes O N lg N average time. This 39 implementation uses median selection to avoid the traditional O N**2 40 worst-case behavior. 41 42 The hheeaappssoorrtt() function is an implementation of J.W.J. William's 43 ``heapsort'' algorithm, a variant of selection sorting; in particular, 44 see D.E. Knuth's Algorithm H. HHeeaappssoorrtt() takes O N lg N worst-case time. 45 Its _o_n_l_y advantage over qqssoorrtt() is that it uses no additional memory. 46 47RREETTUURRNN VVAALLUUEESS 48 The qqssoorrtt() function returns no value. 49 50 Upon successful completion, hheeaappssoorrtt() returns 0. Otherwise, it returns 51 -1 and the global variable _e_r_r_n_o is set to indicate the error. 52 53EERRRROORRSS 54 The hheeaappssoorrtt() function succeeds unless: 55 56 [EINVAL] The _s_i_z_e argument is zero. 57 58CCOOMMPPAATTIIBBIILLIITTYY 59 Previous versions of qqssoorrtt() did not permit the comparison routine to 60 itself call qqssoorrtt(_3). This is no longer true. 61 62SSEEEE AALLSSOO 63 sort(1), radixsort(3) 64 65 66 Hoare, C.A.R., "Quicksort", _T_h_e _C_o_m_p_u_t_e_r _J_o_u_r_n_a_l, 5:1, pp. 10-15, 1962. 67 68 Williams, J.W.J, "Heapsort", _C_o_m_m_u_n_i_c_a_t_i_o_n_s _o_f _t_h_e _A_C_M, 7:1, pp. 347-348, 69 1964. 70 71 Knuth, D.E., "Sorting and Searching", _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, 72 Vol. 3, pp. 114-123, 145-149, 1968. 73 74SSTTAANNDDAARRDDSS 75 The qqssoorrtt() function conforms to ANSI C3.159-1989 (``ANSI C''). 76 77BSD Experimental June 29, 1991 2 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133