xref: /minix3/lib/libc/stdlib/radixsort.3 (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras.\"	$NetBSD: radixsort.3,v 1.13 2003/08/07 16:43:43 agc Exp $
2*2fe8fb19SBen Gras.\"
3*2fe8fb19SBen Gras.\" Copyright (c) 1990, 1991, 1993
4*2fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
5*2fe8fb19SBen Gras.\"
6*2fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
7*2fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
8*2fe8fb19SBen Gras.\" are met:
9*2fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
10*2fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
11*2fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
12*2fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
13*2fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
14*2fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
15*2fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
16*2fe8fb19SBen Gras.\"    without specific prior written permission.
17*2fe8fb19SBen Gras.\"
18*2fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19*2fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*2fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*2fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22*2fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*2fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*2fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*2fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*2fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*2fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*2fe8fb19SBen Gras.\" SUCH DAMAGE.
29*2fe8fb19SBen Gras.\"
30*2fe8fb19SBen Gras.\"     from: @(#)radixsort.3	8.2 (Berkeley) 1/27/94
31*2fe8fb19SBen Gras.\"
32*2fe8fb19SBen Gras.Dd January 27, 1994
33*2fe8fb19SBen Gras.Dt RADIXSORT 3
34*2fe8fb19SBen Gras.Os
35*2fe8fb19SBen Gras.Sh NAME
36*2fe8fb19SBen Gras.Nm radixsort ,
37*2fe8fb19SBen Gras.Nm sradixsort
38*2fe8fb19SBen Gras.Nd radix sort
39*2fe8fb19SBen Gras.Sh LIBRARY
40*2fe8fb19SBen Gras.Lb libc
41*2fe8fb19SBen Gras.Sh SYNOPSIS
42*2fe8fb19SBen Gras.In limits.h
43*2fe8fb19SBen Gras.In stdlib.h
44*2fe8fb19SBen Gras.Ft int
45*2fe8fb19SBen Gras.Fn radixsort "const u_char **base" "int nmemb" "u_char *table" "u_int endbyte"
46*2fe8fb19SBen Gras.Ft int
47*2fe8fb19SBen Gras.Fn sradixsort "const u_char **base" "int nmemb" "u_char *table" "u_int endbyte"
48*2fe8fb19SBen Gras.Sh DESCRIPTION
49*2fe8fb19SBen GrasThe
50*2fe8fb19SBen Gras.Fn radixsort
51*2fe8fb19SBen Grasand
52*2fe8fb19SBen Gras.Fn sradixsort
53*2fe8fb19SBen Grasfunctions
54*2fe8fb19SBen Grasare implementations of radix sort.
55*2fe8fb19SBen Gras.Pp
56*2fe8fb19SBen GrasThese functions sort an
57*2fe8fb19SBen Gras.Fa nmemb
58*2fe8fb19SBen Graselement array of pointers to byte strings, with
59*2fe8fb19SBen Grasthe initial member of which is referenced by
60*2fe8fb19SBen Gras.Fa base .
61*2fe8fb19SBen GrasThe byte strings may contain any values.
62*2fe8fb19SBen GrasEnd of strings is denoted
63*2fe8fb19SBen Grasby character which has same weight as user specified value
64*2fe8fb19SBen Gras.Fa endbyte .
65*2fe8fb19SBen Gras.Fa endbyte
66*2fe8fb19SBen Grashas to be between 0 and 255.
67*2fe8fb19SBen Gras.Pp
68*2fe8fb19SBen GrasApplications may specify a sort order by providing the
69*2fe8fb19SBen Gras.Fa table
70*2fe8fb19SBen Grasargument.
71*2fe8fb19SBen GrasIf
72*2fe8fb19SBen Gras.Pf non- Dv NULL ,
73*2fe8fb19SBen Gras.Fa table
74*2fe8fb19SBen Grasmust reference an array of
75*2fe8fb19SBen Gras.Dv UCHAR_MAX
76*2fe8fb19SBen Gras+ 1 bytes which contains the sort
77*2fe8fb19SBen Grasweight of each possible byte value.
78*2fe8fb19SBen GrasThe end-of-string byte must have a sort weight of 0 or 255
79*2fe8fb19SBen Gras(for sorting in reverse order).
80*2fe8fb19SBen GrasMore than one byte may have the same sort weight.
81*2fe8fb19SBen GrasThe
82*2fe8fb19SBen Gras.Fa table
83*2fe8fb19SBen Grasargument
84*2fe8fb19SBen Grasis useful for applications which wish to sort different characters
85*2fe8fb19SBen Grasequally, for example, providing a table with the same weights
86*2fe8fb19SBen Grasfor A-Z as for a-z will result in a case-insensitive sort.
87*2fe8fb19SBen GrasIf
88*2fe8fb19SBen Gras.Fa table
89*2fe8fb19SBen Grasis NULL, the contents of the array are sorted in ascending order
90*2fe8fb19SBen Grasaccording to the
91*2fe8fb19SBen Gras.Tn ASCII
92*2fe8fb19SBen Grasorder of the byte strings they reference and
93*2fe8fb19SBen Gras.Fa endbyte
94*2fe8fb19SBen Grashas a sorting weight of 0.
95*2fe8fb19SBen Gras.Pp
96*2fe8fb19SBen GrasThe
97*2fe8fb19SBen Gras.Fn sradixsort
98*2fe8fb19SBen Grasfunction is stable, that is, if two elements compare as equal, their
99*2fe8fb19SBen Grasorder in the sorted array is unchanged.
100*2fe8fb19SBen GrasThe
101*2fe8fb19SBen Gras.Fn sradixsort
102*2fe8fb19SBen Grasfunction uses additional memory sufficient to hold
103*2fe8fb19SBen Gras.Fa nmemb
104*2fe8fb19SBen Graspointers.
105*2fe8fb19SBen Gras.Pp
106*2fe8fb19SBen GrasThe
107*2fe8fb19SBen Gras.Fn radixsort
108*2fe8fb19SBen Grasfunction is not stable, but uses no additional memory.
109*2fe8fb19SBen Gras.Pp
110*2fe8fb19SBen GrasThese functions are variants of most-significant-byte radix sorting; in
111*2fe8fb19SBen Grasparticular, see D.E. Knuth's Algorithm R and section 5.2.5, exercise 10.
112*2fe8fb19SBen GrasThey take linear time relative to the number of bytes in the strings.
113*2fe8fb19SBen Gras.Sh RETURN VALUES
114*2fe8fb19SBen GrasUpon successful completion 0 is returned.
115*2fe8fb19SBen GrasOtherwise, \-1 is returned and the global variable
116*2fe8fb19SBen Gras.Va errno
117*2fe8fb19SBen Grasis set to indicate the error.
118*2fe8fb19SBen Gras.Sh ERRORS
119*2fe8fb19SBen Gras.Bl -tag -width Er
120*2fe8fb19SBen Gras.It Bq Er EINVAL
121*2fe8fb19SBen GrasThe value of the
122*2fe8fb19SBen Gras.Fa endbyte
123*2fe8fb19SBen Graselement of
124*2fe8fb19SBen Gras.Fa table
125*2fe8fb19SBen Grasis not 0 or 255.
126*2fe8fb19SBen Gras.El
127*2fe8fb19SBen Gras.Pp
128*2fe8fb19SBen GrasAdditionally, the
129*2fe8fb19SBen Gras.Fn sradixsort
130*2fe8fb19SBen Grasfunction
131*2fe8fb19SBen Grasmay fail and set
132*2fe8fb19SBen Gras.Va errno
133*2fe8fb19SBen Grasfor any of the errors specified for the library routine
134*2fe8fb19SBen Gras.Xr malloc 3 .
135*2fe8fb19SBen Gras.Sh SEE ALSO
136*2fe8fb19SBen Gras.Xr sort 1 ,
137*2fe8fb19SBen Gras.Xr qsort 3
138*2fe8fb19SBen Gras.Pp
139*2fe8fb19SBen Gras.Rs
140*2fe8fb19SBen Gras.%A Knuth, D.E.
141*2fe8fb19SBen Gras.%D 1968
142*2fe8fb19SBen Gras.%B "The Art of Computer Programming"
143*2fe8fb19SBen Gras.%T "Sorting and Searching"
144*2fe8fb19SBen Gras.%V Vol. 3
145*2fe8fb19SBen Gras.%P pp. 170-178
146*2fe8fb19SBen Gras.Re
147*2fe8fb19SBen Gras.Rs
148*2fe8fb19SBen Gras.%A Paige, R.
149*2fe8fb19SBen Gras.%D 1987
150*2fe8fb19SBen Gras.%T "Three Partition Refinement Algorithms"
151*2fe8fb19SBen Gras.%J "SIAM J. Comput."
152*2fe8fb19SBen Gras.%V Vol. 16
153*2fe8fb19SBen Gras.%N No. 6
154*2fe8fb19SBen Gras.Re
155*2fe8fb19SBen Gras.Rs
156*2fe8fb19SBen Gras.%A McIlroy, P.
157*2fe8fb19SBen Gras.%D 1993
158*2fe8fb19SBen Gras.%B "Engineering Radix Sort"
159*2fe8fb19SBen Gras.%T "Computing Systems"
160*2fe8fb19SBen Gras.%V Vol. 6:1
161*2fe8fb19SBen Gras.%P pp. 5-27
162*2fe8fb19SBen Gras.Re
163*2fe8fb19SBen Gras.Sh HISTORY
164*2fe8fb19SBen GrasThe
165*2fe8fb19SBen Gras.Fn radixsort
166*2fe8fb19SBen Grasfunction first appeared in
167*2fe8fb19SBen Gras.Bx 4.4 .
168