xref: /freebsd/lib/libc/stdlib/radixsort.3 (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
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.
12*580b4d18SEd Maste.\" 3. Neither the name of the University nor the names of its contributors
1358f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1458f0484fSRodney W. Grimes.\"    without specific prior written permission.
1558f0484fSRodney W. Grimes.\"
1658f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1758f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1858f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1958f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2058f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2158f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2258f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2358f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2458f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2558f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2658f0484fSRodney W. Grimes.\" SUCH DAMAGE.
2758f0484fSRodney W. Grimes.\"
2858f0484fSRodney W. Grimes.Dd January 27, 1994
2958f0484fSRodney W. Grimes.Dt RADIXSORT 3
3058f0484fSRodney W. Grimes.Os
3158f0484fSRodney W. Grimes.Sh NAME
32cb30b9c5SRuslan Ermilov.Nm radixsort , sradixsort
3358f0484fSRodney W. Grimes.Nd radix sort
3425bb73e0SAlexey Zelkin.Sh LIBRARY
3525bb73e0SAlexey Zelkin.Lb libc
3658f0484fSRodney W. Grimes.Sh SYNOPSIS
378aefde06SJeroen Ruigrok van der Werven.In limits.h
388aefde06SJeroen Ruigrok van der Werven.In stdlib.h
3958f0484fSRodney W. Grimes.Ft int
40a3315650SBruce Evans.Fn radixsort "const unsigned char **base" "int nmemb" "const unsigned char *table" "unsigned endbyte"
4158f0484fSRodney W. Grimes.Ft int
42a3315650SBruce Evans.Fn sradixsort "const unsigned char **base" "int nmemb" "const unsigned char *table" "unsigned endbyte"
4358f0484fSRodney W. Grimes.Sh DESCRIPTION
4458f0484fSRodney W. GrimesThe
4558f0484fSRodney W. Grimes.Fn radixsort
4658f0484fSRodney W. Grimesand
4758f0484fSRodney W. Grimes.Fn sradixsort
4858f0484fSRodney W. Grimesfunctions
4958f0484fSRodney W. Grimesare implementations of radix sort.
5058f0484fSRodney W. Grimes.Pp
5158f0484fSRodney W. GrimesThese functions sort an array of pointers to byte strings, the initial
5258f0484fSRodney W. Grimesmember of which is referenced by
5358f0484fSRodney W. Grimes.Fa base .
5458f0484fSRodney W. GrimesThe byte strings may contain any values; the end of each string
5558f0484fSRodney W. Grimesis denoted by the user-specified value
5658f0484fSRodney W. Grimes.Fa endbyte .
5758f0484fSRodney W. Grimes.Pp
5858f0484fSRodney W. GrimesApplications may specify a sort order by providing the
5958f0484fSRodney W. Grimes.Fa table
6058f0484fSRodney W. Grimesargument.
6158f0484fSRodney W. GrimesIf
6258f0484fSRodney W. Grimes.Pf non- Dv NULL ,
6358f0484fSRodney W. Grimes.Fa table
6458f0484fSRodney W. Grimesmust reference an array of
6558f0484fSRodney W. Grimes.Dv UCHAR_MAX
6658f0484fSRodney W. Grimes+ 1 bytes which contains the sort
6758f0484fSRodney W. Grimesweight of each possible byte value.
6858f0484fSRodney W. GrimesThe end-of-string byte must have a sort weight of 0 or 255
6958f0484fSRodney W. Grimes(for sorting in reverse order).
7058f0484fSRodney W. GrimesMore than one byte may have the same sort weight.
7158f0484fSRodney W. GrimesThe
7258f0484fSRodney W. Grimes.Fa table
7358f0484fSRodney W. Grimesargument
7458f0484fSRodney W. Grimesis useful for applications which wish to sort different characters
7558f0484fSRodney W. Grimesequally, for example, providing a table with the same weights
7658f0484fSRodney W. Grimesfor A-Z as for a-z will result in a case-insensitive sort.
7758f0484fSRodney W. GrimesIf
7858f0484fSRodney W. Grimes.Fa table
7958f0484fSRodney W. Grimesis NULL, the contents of the array are sorted in ascending order
8058f0484fSRodney W. Grimesaccording to the
8158f0484fSRodney W. Grimes.Tn ASCII
8258f0484fSRodney W. Grimesorder of the byte strings they reference and
8358f0484fSRodney W. Grimes.Fa endbyte
8458f0484fSRodney W. Grimeshas a sorting weight of 0.
8558f0484fSRodney W. Grimes.Pp
8658f0484fSRodney W. GrimesThe
8758f0484fSRodney W. Grimes.Fn sradixsort
8858f0484fSRodney W. Grimesfunction is stable, that is, if two elements compare as equal, their
8958f0484fSRodney W. Grimesorder in the sorted array is unchanged.
9058f0484fSRodney W. GrimesThe
9158f0484fSRodney W. Grimes.Fn sradixsort
9258f0484fSRodney W. Grimesfunction uses additional memory sufficient to hold
9358f0484fSRodney W. Grimes.Fa nmemb
9458f0484fSRodney W. Grimespointers.
9558f0484fSRodney W. Grimes.Pp
9658f0484fSRodney W. GrimesThe
9758f0484fSRodney W. Grimes.Fn radixsort
9858f0484fSRodney W. Grimesfunction is not stable, but uses no additional memory.
9958f0484fSRodney W. Grimes.Pp
10058f0484fSRodney W. GrimesThese functions are variants of most-significant-byte radix sorting; in
1011a0a9345SRuslan Ermilovparticular, see
1021a0a9345SRuslan Ermilov.An "D.E. Knuth" Ns 's
1031a0a9345SRuslan Ermilov.%T "Algorithm R"
1041a0a9345SRuslan Ermilovand section 5.2.5, exercise 10.
10558f0484fSRodney W. GrimesThey take linear time relative to the number of bytes in the strings.
10658f0484fSRodney W. Grimes.Sh RETURN VALUES
107b1250632SYaroslav Tykhiy.Rv -std radixsort
10858f0484fSRodney W. Grimes.Sh ERRORS
10958f0484fSRodney W. Grimes.Bl -tag -width Er
11058f0484fSRodney W. Grimes.It Bq Er EINVAL
11158f0484fSRodney W. GrimesThe value of the
11258f0484fSRodney W. Grimes.Fa endbyte
11358f0484fSRodney W. Grimeselement of
11458f0484fSRodney W. Grimes.Fa table
11558f0484fSRodney W. Grimesis not 0 or 255.
11658f0484fSRodney W. Grimes.El
11758f0484fSRodney W. Grimes.Pp
11858f0484fSRodney W. GrimesAdditionally, the
11958f0484fSRodney W. Grimes.Fn sradixsort
12058f0484fSRodney W. Grimesfunction
12158f0484fSRodney W. Grimesmay fail and set
12258f0484fSRodney W. Grimes.Va errno
12358f0484fSRodney W. Grimesfor any of the errors specified for the library routine
12458f0484fSRodney W. Grimes.Xr malloc 3 .
12558f0484fSRodney W. Grimes.Sh SEE ALSO
12658f0484fSRodney W. Grimes.Xr sort 1 ,
12758f0484fSRodney W. Grimes.Xr qsort 3
12858f0484fSRodney W. Grimes.Pp
12958f0484fSRodney W. Grimes.Rs
13058f0484fSRodney W. Grimes.%A Knuth, D.E.
13158f0484fSRodney W. Grimes.%D 1968
13258f0484fSRodney W. Grimes.%B "The Art of Computer Programming"
13358f0484fSRodney W. Grimes.%T "Sorting and Searching"
13458f0484fSRodney W. Grimes.%V Vol. 3
13558f0484fSRodney W. Grimes.%P pp. 170-178
13658f0484fSRodney W. Grimes.Re
13758f0484fSRodney W. Grimes.Rs
13858f0484fSRodney W. Grimes.%A Paige, R.
13958f0484fSRodney W. Grimes.%D 1987
14058f0484fSRodney W. Grimes.%T "Three Partition Refinement Algorithms"
14158f0484fSRodney W. Grimes.%J "SIAM J. Comput."
14258f0484fSRodney W. Grimes.%V Vol. 16
14358f0484fSRodney W. Grimes.%N No. 6
14458f0484fSRodney W. Grimes.Re
14558f0484fSRodney W. Grimes.Rs
14658f0484fSRodney W. Grimes.%A McIlroy, P.
14758f0484fSRodney W. Grimes.%D 1993
14858f0484fSRodney W. Grimes.%B "Engineering Radix Sort"
14958f0484fSRodney W. Grimes.%T "Computing Systems"
15058f0484fSRodney W. Grimes.%V Vol. 6:1
15158f0484fSRodney W. Grimes.%P pp. 5-27
15258f0484fSRodney W. Grimes.Re
15358f0484fSRodney W. Grimes.Sh HISTORY
15458f0484fSRodney W. GrimesThe
15558f0484fSRodney W. Grimes.Fn radixsort
1567bdf80e5SMike Pritchardfunction first appeared in
1577bdf80e5SMike Pritchard.Bx 4.4 .
158