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