1.\" $NetBSD: radixsort.3,v 1.4 1995/12/28 08:52:37 thorpej 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 SYNOPSIS 43.Fd #include <limits.h> 44.Fd #include <stdlib.h> 45.Ft int 46.Fn radixsort "u_char **base" "int nmemb" "u_char *table" "u_int endbyte" 47.Ft int 48.Fn sradixsort "u_char **base" "int nmemb" "u_char *table" "u_int endbyte" 49.Sh DESCRIPTION 50The 51.Fn radixsort 52and 53.Fn sradixsort 54functions 55are implementations of radix sort. 56.Pp 57These functions sort an array of pointers to byte strings, the initial 58member of which is referenced by 59.Fa base . 60The byte strings may contain any values; the end of each string 61is denoted by the user-specified value 62.Fa endbyte . 63.Pp 64Applications may specify a sort order by providing the 65.Fa table 66argument. 67If 68.Pf non- Dv NULL , 69.Fa table 70must reference an array of 71.Dv UCHAR_MAX 72+ 1 bytes which contains the sort 73weight of each possible byte value. 74The end-of-string byte must have a sort weight of 0 or 255 75(for sorting in reverse order). 76More than one byte may have the same sort weight. 77The 78.Fa table 79argument 80is useful for applications which wish to sort different characters 81equally, for example, providing a table with the same weights 82for A-Z as for a-z will result in a case-insensitive sort. 83If 84.Fa table 85is NULL, the contents of the array are sorted in ascending order 86according to the 87.Tn ASCII 88order of the byte strings they reference and 89.Fa endbyte 90has a sorting weight of 0. 91.Pp 92The 93.Fn sradixsort 94function is stable, that is, if two elements compare as equal, their 95order in the sorted array is unchanged. 96The 97.Fn sradixsort 98function uses additional memory sufficient to hold 99.Fa nmemb 100pointers. 101.Pp 102The 103.Fn radixsort 104function is not stable, but uses no additional memory. 105.Pp 106These functions are variants of most-significant-byte radix sorting; in 107particular, see D.E. Knuth's Algorithm R and section 5.2.5, exercise 10. 108They take linear time relative to the number of bytes in the strings. 109.Sh RETURN VALUES 110Upon successful completion 0 is returned. 111Otherwise, \-1 is returned and the global variable 112.Va errno 113is set to indicate the error. 114.Sh ERRORS 115.Bl -tag -width Er 116.It Bq Er EINVAL 117The value of the 118.Fa endbyte 119element of 120.Fa table 121is not 0 or 255. 122.El 123.Pp 124Additionally, the 125.Fn sradixsort 126function 127may fail and set 128.Va errno 129for any of the errors specified for the library routine 130.Xr malloc 3 . 131.Sh SEE ALSO 132.Xr sort 1 , 133.Xr qsort 3 134.Pp 135.Rs 136.%A Knuth, D.E. 137.%D 1968 138.%B "The Art of Computer Programming" 139.%T "Sorting and Searching" 140.%V Vol. 3 141.%P pp. 170-178 142.Re 143.Rs 144.%A Paige, R. 145.%D 1987 146.%T "Three Partition Refinement Algorithms" 147.%J "SIAM J. Comput." 148.%V Vol. 16 149.%N No. 6 150.Re 151.Rs 152.%A McIlroy, P. 153.%D 1993 154.%B "Engineering Radix Sort" 155.%T "Computing Systems" 156.%V Vol. 6:1 157.%P pp. 5-27 158.Re 159.Sh HISTORY 160The 161.Fn radixsort 162function first appeared in 4.4BSD. 163