xref: /freebsd/lib/libc/stdlib/radixsort.3 (revision 25bb73e063c17cd9048cf60100dbc0ac5177e94a)
158f0484fSRodney W. Grimes.\" Copyright (c) 1990, 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
658f0484fSRodney W. Grimes.\" are met:
758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
858f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1058f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1158f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
1258f0484fSRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software
1358f0484fSRodney W. Grimes.\"    must display the following acknowledgement:
1458f0484fSRodney W. Grimes.\"	This product includes software developed by the University of
1558f0484fSRodney W. Grimes.\"	California, Berkeley and its contributors.
1658f0484fSRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes.\"    without specific prior written permission.
1958f0484fSRodney W. Grimes.\"
2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes.\" SUCH DAMAGE.
3158f0484fSRodney W. Grimes.\"
3258f0484fSRodney W. Grimes.\"     @(#)radixsort.3	8.2 (Berkeley) 1/27/94
337f3dea24SPeter Wemm.\" $FreeBSD$
3458f0484fSRodney W. Grimes.\"
3558f0484fSRodney W. Grimes.Dd January 27, 1994
3658f0484fSRodney W. Grimes.Dt RADIXSORT 3
3758f0484fSRodney W. Grimes.Os
3858f0484fSRodney W. Grimes.Sh NAME
3958f0484fSRodney W. Grimes.Nm radixsort
4058f0484fSRodney W. Grimes.Nd radix sort
4125bb73e0SAlexey Zelkin.Sh LIBRARY
4225bb73e0SAlexey Zelkin.Lb libc
4358f0484fSRodney W. Grimes.Sh SYNOPSIS
4458f0484fSRodney W. Grimes.Fd #include <limits.h>
4558f0484fSRodney W. Grimes.Fd #include <stdlib.h>
4658f0484fSRodney W. Grimes.Ft int
47a3315650SBruce Evans.Fn radixsort "const unsigned char **base" "int nmemb" "const unsigned char *table" "unsigned endbyte"
4858f0484fSRodney W. Grimes.Ft int
49a3315650SBruce Evans.Fn sradixsort "const unsigned char **base" "int nmemb" "const unsigned char *table" "unsigned endbyte"
5058f0484fSRodney W. Grimes.Sh DESCRIPTION
5158f0484fSRodney W. GrimesThe
5258f0484fSRodney W. Grimes.Fn radixsort
5358f0484fSRodney W. Grimesand
5458f0484fSRodney W. Grimes.Fn sradixsort
5558f0484fSRodney W. Grimesfunctions
5658f0484fSRodney W. Grimesare implementations of radix sort.
5758f0484fSRodney W. Grimes.Pp
5858f0484fSRodney W. GrimesThese functions sort an array of pointers to byte strings, the initial
5958f0484fSRodney W. Grimesmember of which is referenced by
6058f0484fSRodney W. Grimes.Fa base .
6158f0484fSRodney W. GrimesThe byte strings may contain any values; the end of each string
6258f0484fSRodney W. Grimesis denoted by the user-specified value
6358f0484fSRodney W. Grimes.Fa endbyte .
6458f0484fSRodney W. Grimes.Pp
6558f0484fSRodney W. GrimesApplications may specify a sort order by providing the
6658f0484fSRodney W. Grimes.Fa table
6758f0484fSRodney W. Grimesargument.
6858f0484fSRodney W. GrimesIf
6958f0484fSRodney W. Grimes.Pf non- Dv NULL ,
7058f0484fSRodney W. Grimes.Fa table
7158f0484fSRodney W. Grimesmust reference an array of
7258f0484fSRodney W. Grimes.Dv UCHAR_MAX
7358f0484fSRodney W. Grimes+ 1 bytes which contains the sort
7458f0484fSRodney W. Grimesweight of each possible byte value.
7558f0484fSRodney W. GrimesThe end-of-string byte must have a sort weight of 0 or 255
7658f0484fSRodney W. Grimes(for sorting in reverse order).
7758f0484fSRodney W. GrimesMore than one byte may have the same sort weight.
7858f0484fSRodney W. GrimesThe
7958f0484fSRodney W. Grimes.Fa table
8058f0484fSRodney W. Grimesargument
8158f0484fSRodney W. Grimesis useful for applications which wish to sort different characters
8258f0484fSRodney W. Grimesequally, for example, providing a table with the same weights
8358f0484fSRodney W. Grimesfor A-Z as for a-z will result in a case-insensitive sort.
8458f0484fSRodney W. GrimesIf
8558f0484fSRodney W. Grimes.Fa table
8658f0484fSRodney W. Grimesis NULL, the contents of the array are sorted in ascending order
8758f0484fSRodney W. Grimesaccording to the
8858f0484fSRodney W. Grimes.Tn ASCII
8958f0484fSRodney W. Grimesorder of the byte strings they reference and
9058f0484fSRodney W. Grimes.Fa endbyte
9158f0484fSRodney W. Grimeshas a sorting weight of 0.
9258f0484fSRodney W. Grimes.Pp
9358f0484fSRodney W. GrimesThe
9458f0484fSRodney W. Grimes.Fn sradixsort
9558f0484fSRodney W. Grimesfunction is stable, that is, if two elements compare as equal, their
9658f0484fSRodney W. Grimesorder in the sorted array is unchanged.
9758f0484fSRodney W. GrimesThe
9858f0484fSRodney W. Grimes.Fn sradixsort
9958f0484fSRodney W. Grimesfunction uses additional memory sufficient to hold
10058f0484fSRodney W. Grimes.Fa nmemb
10158f0484fSRodney W. Grimespointers.
10258f0484fSRodney W. Grimes.Pp
10358f0484fSRodney W. GrimesThe
10458f0484fSRodney W. Grimes.Fn radixsort
10558f0484fSRodney W. Grimesfunction is not stable, but uses no additional memory.
10658f0484fSRodney W. Grimes.Pp
10758f0484fSRodney W. GrimesThese functions are variants of most-significant-byte radix sorting; in
10858f0484fSRodney W. Grimesparticular, see D.E. Knuth's Algorithm R and section 5.2.5, exercise 10.
10958f0484fSRodney W. GrimesThey take linear time relative to the number of bytes in the strings.
11058f0484fSRodney W. Grimes.Sh RETURN VALUES
11158f0484fSRodney W. GrimesUpon successful completion 0 is returned.
11258f0484fSRodney W. GrimesOtherwise, \-1 is returned and the global variable
11358f0484fSRodney W. Grimes.Va errno
11458f0484fSRodney W. Grimesis set to indicate the error.
11558f0484fSRodney W. Grimes.Sh ERRORS
11658f0484fSRodney W. Grimes.Bl -tag -width Er
11758f0484fSRodney W. Grimes.It Bq Er EINVAL
11858f0484fSRodney W. GrimesThe value of the
11958f0484fSRodney W. Grimes.Fa endbyte
12058f0484fSRodney W. Grimeselement of
12158f0484fSRodney W. Grimes.Fa table
12258f0484fSRodney W. Grimesis not 0 or 255.
12358f0484fSRodney W. Grimes.El
12458f0484fSRodney W. Grimes.Pp
12558f0484fSRodney W. GrimesAdditionally, the
12658f0484fSRodney W. Grimes.Fn sradixsort
12758f0484fSRodney W. Grimesfunction
12858f0484fSRodney W. Grimesmay fail and set
12958f0484fSRodney W. Grimes.Va errno
13058f0484fSRodney W. Grimesfor any of the errors specified for the library routine
13158f0484fSRodney W. Grimes.Xr malloc 3 .
13258f0484fSRodney W. Grimes.Sh SEE ALSO
13358f0484fSRodney W. Grimes.Xr sort 1 ,
13458f0484fSRodney W. Grimes.Xr qsort 3
13558f0484fSRodney W. Grimes.Pp
13658f0484fSRodney W. Grimes.Rs
13758f0484fSRodney W. Grimes.%A Knuth, D.E.
13858f0484fSRodney W. Grimes.%D 1968
13958f0484fSRodney W. Grimes.%B "The Art of Computer Programming"
14058f0484fSRodney W. Grimes.%T "Sorting and Searching"
14158f0484fSRodney W. Grimes.%V Vol. 3
14258f0484fSRodney W. Grimes.%P pp. 170-178
14358f0484fSRodney W. Grimes.Re
14458f0484fSRodney W. Grimes.Rs
14558f0484fSRodney W. Grimes.%A Paige, R.
14658f0484fSRodney W. Grimes.%D 1987
14758f0484fSRodney W. Grimes.%T "Three Partition Refinement Algorithms"
14858f0484fSRodney W. Grimes.%J "SIAM J. Comput."
14958f0484fSRodney W. Grimes.%V Vol. 16
15058f0484fSRodney W. Grimes.%N No. 6
15158f0484fSRodney W. Grimes.Re
15258f0484fSRodney W. Grimes.Rs
15358f0484fSRodney W. Grimes.%A McIlroy, P.
15458f0484fSRodney W. Grimes.%D 1993
15558f0484fSRodney W. Grimes.%B "Engineering Radix Sort"
15658f0484fSRodney W. Grimes.%T "Computing Systems"
15758f0484fSRodney W. Grimes.%V Vol. 6:1
15858f0484fSRodney W. Grimes.%P pp. 5-27
15958f0484fSRodney W. Grimes.Re
16058f0484fSRodney W. Grimes.Sh HISTORY
16158f0484fSRodney W. GrimesThe
16258f0484fSRodney W. Grimes.Fn radixsort
1637bdf80e5SMike Pritchardfunction first appeared in
1647bdf80e5SMike Pritchard.Bx 4.4 .
165