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