xref: /netbsd-src/lib/libc/stdlib/radixsort.3 (revision eb7c1594f145c931049e1fd9eb056a5987e87e59)
1*eb7c1594Sagc.\"	$NetBSD: radixsort.3,v 1.13 2003/08/07 16:43:43 agc Exp $
26dda330eSthorpej.\"
32f86deeaSmycroft.\" Copyright (c) 1990, 1991, 1993
42f86deeaSmycroft.\"	The Regents of the University of California.  All rights reserved.
561f28255Scgd.\"
661f28255Scgd.\" Redistribution and use in source and binary forms, with or without
761f28255Scgd.\" modification, are permitted provided that the following conditions
861f28255Scgd.\" are met:
961f28255Scgd.\" 1. Redistributions of source code must retain the above copyright
1061f28255Scgd.\"    notice, this list of conditions and the following disclaimer.
1161f28255Scgd.\" 2. Redistributions in binary form must reproduce the above copyright
1261f28255Scgd.\"    notice, this list of conditions and the following disclaimer in the
1361f28255Scgd.\"    documentation and/or other materials provided with the distribution.
14*eb7c1594Sagc.\" 3. Neither the name of the University nor the names of its contributors
1561f28255Scgd.\"    may be used to endorse or promote products derived from this software
1661f28255Scgd.\"    without specific prior written permission.
1761f28255Scgd.\"
1861f28255Scgd.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1961f28255Scgd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2061f28255Scgd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2161f28255Scgd.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2261f28255Scgd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2361f28255Scgd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2461f28255Scgd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2561f28255Scgd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2661f28255Scgd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2761f28255Scgd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2861f28255Scgd.\" SUCH DAMAGE.
2961f28255Scgd.\"
302f86deeaSmycroft.\"     from: @(#)radixsort.3	8.2 (Berkeley) 1/27/94
3161f28255Scgd.\"
322f86deeaSmycroft.Dd January 27, 1994
3361f28255Scgd.Dt RADIXSORT 3
3461f28255Scgd.Os
3561f28255Scgd.Sh NAME
368e610505Ssimonb.Nm radixsort ,
378e610505Ssimonb.Nm sradixsort
3861f28255Scgd.Nd radix sort
39312aca53Sperry.Sh LIBRARY
40312aca53Sperry.Lb libc
4161f28255Scgd.Sh SYNOPSIS
42472351e1Swiz.In limits.h
43472351e1Swiz.In stdlib.h
4461f28255Scgd.Ft int
4541f8bfbcSgarbled.Fn radixsort "const u_char **base" "int nmemb" "u_char *table" "u_int endbyte"
462f86deeaSmycroft.Ft int
4741f8bfbcSgarbled.Fn sradixsort "const u_char **base" "int nmemb" "u_char *table" "u_int endbyte"
4861f28255Scgd.Sh DESCRIPTION
4961f28255ScgdThe
5061f28255Scgd.Fn radixsort
512f86deeaSmycroftand
522f86deeaSmycroft.Fn sradixsort
532f86deeaSmycroftfunctions
542f86deeaSmycroftare implementations of radix sort.
5561f28255Scgd.Pp
560b6f0115SfairThese functions sort an
570b6f0115Sfair.Fa nmemb
580b6f0115Sfairelement array of pointers to byte strings, with
590b6f0115Sfairthe initial member of which is referenced by
6061f28255Scgd.Fa base .
610b6f0115SfairThe byte strings may contain any values.
620b6f0115SfairEnd of strings is denoted
63ae76c71dSjdolecekby character which has same weight as user specified value
6461f28255Scgd.Fa endbyte .
65ae76c71dSjdolecek.Fa endbyte
66ae76c71dSjdolecekhas to be between 0 and 255.
6761f28255Scgd.Pp
6861f28255ScgdApplications may specify a sort order by providing the
6961f28255Scgd.Fa table
7061f28255Scgdargument.
7161f28255ScgdIf
7261f28255Scgd.Pf non- Dv NULL ,
7361f28255Scgd.Fa table
7461f28255Scgdmust reference an array of
7561f28255Scgd.Dv UCHAR_MAX
7661f28255Scgd+ 1 bytes which contains the sort
7761f28255Scgdweight of each possible byte value.
782f86deeaSmycroftThe end-of-string byte must have a sort weight of 0 or 255
792f86deeaSmycroft(for sorting in reverse order).
8061f28255ScgdMore than one byte may have the same sort weight.
8161f28255ScgdThe
8261f28255Scgd.Fa table
8361f28255Scgdargument
8461f28255Scgdis useful for applications which wish to sort different characters
852f86deeaSmycroftequally, for example, providing a table with the same weights
8661f28255Scgdfor A-Z as for a-z will result in a case-insensitive sort.
872f86deeaSmycroftIf
882f86deeaSmycroft.Fa table
892f86deeaSmycroftis NULL, the contents of the array are sorted in ascending order
902f86deeaSmycroftaccording to the
912f86deeaSmycroft.Tn ASCII
922f86deeaSmycroftorder of the byte strings they reference and
932f86deeaSmycroft.Fa endbyte
942f86deeaSmycrofthas a sorting weight of 0.
952f86deeaSmycroft.Pp
962f86deeaSmycroftThe
972f86deeaSmycroft.Fn sradixsort
982f86deeaSmycroftfunction is stable, that is, if two elements compare as equal, their
992f86deeaSmycroftorder in the sorted array is unchanged.
1002f86deeaSmycroftThe
1012f86deeaSmycroft.Fn sradixsort
1022f86deeaSmycroftfunction uses additional memory sufficient to hold
1032f86deeaSmycroft.Fa nmemb
1042f86deeaSmycroftpointers.
10561f28255Scgd.Pp
10661f28255ScgdThe
10761f28255Scgd.Fn radixsort
1082f86deeaSmycroftfunction is not stable, but uses no additional memory.
10961f28255Scgd.Pp
1102f86deeaSmycroftThese functions are variants of most-significant-byte radix sorting; in
1112f86deeaSmycroftparticular, see D.E. Knuth's Algorithm R and section 5.2.5, exercise 10.
1122f86deeaSmycroftThey take linear time relative to the number of bytes in the strings.
11361f28255Scgd.Sh RETURN VALUES
11461f28255ScgdUpon successful completion 0 is returned.
11561f28255ScgdOtherwise, \-1 is returned and the global variable
11661f28255Scgd.Va errno
11761f28255Scgdis set to indicate the error.
11861f28255Scgd.Sh ERRORS
1192f86deeaSmycroft.Bl -tag -width Er
1202f86deeaSmycroft.It Bq Er EINVAL
1212f86deeaSmycroftThe value of the
1222f86deeaSmycroft.Fa endbyte
1232f86deeaSmycroftelement of
1242f86deeaSmycroft.Fa table
1252f86deeaSmycroftis not 0 or 255.
1262f86deeaSmycroft.El
1272f86deeaSmycroft.Pp
1282f86deeaSmycroftAdditionally, the
1292f86deeaSmycroft.Fn sradixsort
13061f28255Scgdfunction
13161f28255Scgdmay fail and set
13261f28255Scgd.Va errno
13361f28255Scgdfor any of the errors specified for the library routine
13461f28255Scgd.Xr malloc 3 .
13561f28255Scgd.Sh SEE ALSO
13661f28255Scgd.Xr sort 1 ,
13761f28255Scgd.Xr qsort 3
13861f28255Scgd.Pp
13961f28255Scgd.Rs
14061f28255Scgd.%A Knuth, D.E.
14161f28255Scgd.%D 1968
14261f28255Scgd.%B "The Art of Computer Programming"
14361f28255Scgd.%T "Sorting and Searching"
14461f28255Scgd.%V Vol. 3
14561f28255Scgd.%P pp. 170-178
14661f28255Scgd.Re
14761f28255Scgd.Rs
14861f28255Scgd.%A Paige, R.
14961f28255Scgd.%D 1987
15061f28255Scgd.%T "Three Partition Refinement Algorithms"
15161f28255Scgd.%J "SIAM J. Comput."
15261f28255Scgd.%V Vol. 16
15361f28255Scgd.%N No. 6
15461f28255Scgd.Re
1552f86deeaSmycroft.Rs
1562f86deeaSmycroft.%A McIlroy, P.
1572f86deeaSmycroft.%D 1993
1582f86deeaSmycroft.%B "Engineering Radix Sort"
1592f86deeaSmycroft.%T "Computing Systems"
1602f86deeaSmycroft.%V Vol. 6:1
1612f86deeaSmycroft.%P pp. 5-27
1622f86deeaSmycroft.Re
16361f28255Scgd.Sh HISTORY
16461f28255ScgdThe
16561f28255Scgd.Fn radixsort
166a16d9e86Sperryfunction first appeared in
167a16d9e86Sperry.Bx 4.4 .
168