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