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